Merge pull request #2101 from Kruna1Pate1/fix/pip-aspect-ratio

Fix to adopt dynamic aspect ration in PiP mode
This commit is contained in:
Bnyro 2022-11-27 14:59:29 +01:00 committed by GitHub
commit dbf4002e62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 1 deletions

View File

@ -0,0 +1,22 @@
package com.github.libretube.ui.extensions
import android.app.PictureInPictureParams
import android.os.Build
import android.util.Rational
import androidx.annotation.RequiresApi
@RequiresApi(Build.VERSION_CODES.O)
fun PictureInPictureParams.Builder.setAspectRatio(
width: Int,
height: Int
): PictureInPictureParams.Builder {
val ratio = (width.toFloat() / height).let {
when {
it.isNaN() -> Rational(4, 3)
it <= 0.418410 -> Rational(41841, 100000)
it >= 2.390000 -> Rational(239, 100)
else -> Rational(width, height)
}
}
return setAspectRatio(ratio)
}

View File

@ -71,6 +71,7 @@ import com.github.libretube.ui.base.BaseFragment
import com.github.libretube.ui.dialogs.AddToPlaylistDialog
import com.github.libretube.ui.dialogs.DownloadDialog
import com.github.libretube.ui.dialogs.ShareDialog
import com.github.libretube.ui.extensions.setAspectRatio
import com.github.libretube.ui.extensions.setFormattedHtml
import com.github.libretube.ui.extensions.setInvisible
import com.github.libretube.ui.extensions.setupSubscriptionButton
@ -277,7 +278,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
binding.playerMotionLayout.transitionToStart()
}
if (usePiP()) (activity as MainActivity).setPictureInPictureParams(getPipParams())
if (usePiP()) activity?.setPictureInPictureParams(getPipParams())
if (SDK_INT < Build.VERSION_CODES.O) {
binding.relPlayerPip.visibility = View.GONE
@ -823,6 +824,8 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
// media actually playing
transitioning = false
binding.playImageView.setImageResource(R.drawable.ic_pause)
// update the PiP params to use the correct aspect ratio
if (usePiP()) activity?.setPictureInPictureParams(getPipParams())
}
Player.STATE_ENDED -> {
// video has finished
@ -1360,6 +1363,9 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
if (SDK_INT >= Build.VERSION_CODES.S) {
setAutoEnterEnabled(true)
}
if (exoPlayer.isPlaying) {
setAspectRatio(exoPlayer.videoSize.width, exoPlayer.videoSize.height)
}
}
.build()