Merge pull request #3795 from Bnyro/master

Fix various Picure in Picture issues
This commit is contained in:
Bnyro 2023-05-18 14:44:04 +02:00 committed by GitHub
commit d70931966d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 31 deletions

View File

@ -4,6 +4,7 @@ import android.app.Activity
import android.content.Context import android.content.Context
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.os.Build import android.os.Build
import android.util.Log
object PictureInPictureCompat { object PictureInPictureCompat {
fun isPictureInPictureAvailable(context: Context): Boolean { fun isPictureInPictureAvailable(context: Context): Boolean {
@ -16,13 +17,14 @@ object PictureInPictureCompat {
} }
fun setPictureInPictureParams(activity: Activity, params: PictureInPictureParamsCompat) { fun setPictureInPictureParams(activity: Activity, params: PictureInPictureParamsCompat) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (isPictureInPictureAvailable(activity)) {
if (params.toPictureInPictureParams().actions.isEmpty()) throw IllegalArgumentException()
activity.setPictureInPictureParams(params.toPictureInPictureParams()) activity.setPictureInPictureParams(params.toPictureInPictureParams())
} }
} }
fun enterPictureInPictureMode(activity: Activity, params: PictureInPictureParamsCompat) { fun enterPictureInPictureMode(activity: Activity, params: PictureInPictureParamsCompat) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (isPictureInPictureAvailable(activity)) {
activity.enterPictureInPictureMode(params.toPictureInPictureParams()) activity.enterPictureInPictureMode(params.toPictureInPictureParams())
} }
} }

View File

@ -18,6 +18,7 @@ import android.text.format.DateUtils
import android.text.method.LinkMovementMethod import android.text.method.LinkMovementMethod
import android.text.util.Linkify import android.text.util.Linkify
import android.util.Base64 import android.util.Base64
import android.util.Log
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
@ -116,7 +117,6 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString import kotlinx.serialization.encodeToString
import retrofit2.HttpException import retrofit2.HttpException
import java.io.IOException import java.io.IOException
@ -601,20 +601,25 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
override fun onDestroy() { override fun onDestroy() {
super.onDestroy() super.onDestroy()
// disable the auto PiP mode for SDK >= 32
exoPlayer.pause()
PictureInPictureCompat.setPictureInPictureParams(
requireActivity(),
pipParams,
)
handler.removeCallbacksAndMessages(null) handler.removeCallbacksAndMessages(null)
try { runCatching {
// disable the auto PiP mode for SDK >= 32
disableAutoPiP()
// unregister the receiver for player actions // unregister the receiver for player actions
context?.unregisterReceiver(broadcastReceiver) context?.unregisterReceiver(broadcastReceiver)
}
try {
saveWatchPosition() saveWatchPosition()
PlayingQueue.clear() PlayingQueue.clear()
// release the player
nowPlayingNotification.destroySelfAndPlayer() nowPlayingNotification.destroySelfAndPlayer()
activity?.requestedOrientation = activity?.requestedOrientation =
@ -630,14 +635,6 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
_binding = null _binding = null
} }
private fun disableAutoPiP() {
// autoEnterEnabled is false by default
PictureInPictureCompat.setPictureInPictureParams(
requireActivity(),
PictureInPictureParamsCompat.Builder().build(),
)
}
// save the watch position if video isn't finished and option enabled // save the watch position if video isn't finished and option enabled
private fun saveWatchPosition() { private fun saveWatchPosition() {
if (!PlayerHelper.watchPositionsVideo) return if (!PlayerHelper.watchPositionsVideo) return
@ -928,8 +925,6 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
if (isPlaying) { if (isPlaying) {
// Stop [BackgroundMode] service if it is running. // Stop [BackgroundMode] service if it is running.
BackgroundHelper.stopBackgroundPlay(requireContext()) BackgroundHelper.stopBackgroundPlay(requireContext())
} else {
disableAutoPiP()
} }
if (isPlaying && PlayerHelper.sponsorBlockEnabled) { if (isPlaying && PlayerHelper.sponsorBlockEnabled) {
@ -974,22 +969,17 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
} }
} }
val activity = requireActivity()
if (playbackState == Player.STATE_READY) { if (playbackState == Player.STATE_READY) {
// media actually playing // media actually playing
transitioning = false transitioning = false
// update the PiP params to use the correct aspect ratio
if (PlayerHelper.pipEnabled) {
PictureInPictureCompat.setPictureInPictureParams(activity, pipParams)
}
} }
// listen for the stop button in the notification // listen for the stop button in the notification
if (playbackState == PlaybackState.STATE_STOPPED && PlayerHelper.pipEnabled && if (playbackState == PlaybackState.STATE_STOPPED && PlayerHelper.pipEnabled &&
PictureInPictureCompat.isInPictureInPictureMode(activity) PictureInPictureCompat.isInPictureInPictureMode(requireActivity())
) { ) {
// finish PiP by finishing the activity // finish PiP by finishing the activity
activity.finish() activity?.finish()
} }
super.onPlaybackStateChanged(playbackState) super.onPlaybackStateChanged(playbackState)
} }
@ -1541,7 +1531,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
private val pipParams private val pipParams
get() = PictureInPictureParamsCompat.Builder() get() = PictureInPictureParamsCompat.Builder()
.setActions(PlayerHelper.getPiPModeActions(requireActivity(), exoPlayer.isPlaying)) .setActions(PlayerHelper.getPiPModeActions(requireActivity(), exoPlayer.isPlaying))
.setAutoEnterEnabled(PlayerHelper.pipEnabled) .setAutoEnterEnabled(PlayerHelper.pipEnabled && exoPlayer.isPlaying)
.apply { .apply {
if (exoPlayer.isPlaying) { if (exoPlayer.isPlaying) {
setAspectRatio(exoPlayer.videoSize) setAspectRatio(exoPlayer.videoSize)
@ -1568,12 +1558,15 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
) )
} }
private fun shouldStartPiP(): Boolean { /**
if (!PlayerHelper.pipEnabled || Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { * Detect whether PiP is supported and enabled
return false */
private fun usePiP(): Boolean {
return PictureInPictureCompat.isPictureInPictureAvailable(requireContext()) && PlayerHelper.pipEnabled
} }
return exoPlayer.isPlaying && !BackgroundHelper.isBackgroundServiceRunning(requireContext()) private fun shouldStartPiP(): Boolean {
return usePiP() && exoPlayer.isPlaying && !BackgroundHelper.isBackgroundServiceRunning(requireContext())
} }
private fun killPlayerFragment() { private fun killPlayerFragment() {