From 1e3734aeb5d727736a561532cfd7a6fa785cd859 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Fri, 24 Apr 2026 21:08:11 +0200 Subject: [PATCH] Fix pip with mpv --- .../src/main/java/dev/zoriya/omni/OmniView.kt | 31 ++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/android/src/main/java/dev/zoriya/omni/OmniView.kt b/android/src/main/java/dev/zoriya/omni/OmniView.kt index 00a21f8..03a6d0b 100755 --- a/android/src/main/java/dev/zoriya/omni/OmniView.kt +++ b/android/src/main/java/dev/zoriya/omni/OmniView.kt @@ -11,6 +11,7 @@ import android.view.View import android.view.ViewGroup import android.widget.FrameLayout import androidx.core.view.isVisible +import androidx.media3.common.Player import com.facebook.react.bridge.LifecycleEventListener import com.facebook.react.uimanager.ThemedReactContext import com.margelo.nitro.omni.HybridOmniPlayerSpec @@ -21,14 +22,17 @@ class OmniView(val context: ThemedReactContext) : HybridOmniViewSpec(), SurfaceHolder.Callback, LifecycleEventListener, - View.OnLayoutChangeListener { + View.OnLayoutChangeListener, + Player.Listener { companion object { private var activeView = WeakReference(null) @Suppress("unused") @JvmStatic fun onActivityPipTransitionToPip(activity: android.app.Activity) { - activeView.get()?.isolateUiForPipIfNeeded(activity) + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) { + activeView.get()?.isolateUiForPipIfNeeded(activity) + } } @Suppress("unused") @@ -38,9 +42,7 @@ class OmniView(val context: ThemedReactContext) : isInPictureInPictureMode: Boolean ) { if (isInPictureInPictureMode) { - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.VANILLA_ICE_CREAM) { - activeView.get()?.isolateUiForPipIfNeeded(activity) - } + activeView.get()?.isolateUiForPipIfNeeded(activity, afterEnteredPip = true) } else { activeView.get()?.restoreUiAfterPip() } @@ -135,8 +137,10 @@ class OmniView(val context: ThemedReactContext) : return } + boundPlayer?.player?.removeListener(this) boundPlayer?.setSurface(null) boundPlayer = omniPlayer + omniPlayer.player.addListener(this) if (surfaceReady) { omniPlayer.setSurface(surfaceView.holder) @@ -158,6 +162,7 @@ class OmniView(val context: ThemedReactContext) : if (!::player.isInitialized) return val omniPlayer = player as? OmniPlayer ?: return + boundPlayer?.player?.removeListener(this) omniPlayer.setSurface(null) boundPlayer = null } @@ -195,6 +200,14 @@ class OmniView(val context: ThemedReactContext) : restoreUiAfterPip() } + override fun onIsPlayingChanged(isPlaying: Boolean) { + updatePictureInPictureParams() + } + + override fun onPlaybackStateChanged(playbackState: Int) { + updatePictureInPictureParams() + } + private fun updatePictureInPictureParams() { val activity = context.currentActivity ?: return if (activity.isFinishing || activity.isDestroyed || movedSurfaceToRootForPip) { @@ -233,10 +246,14 @@ class OmniView(val context: ThemedReactContext) : return builder.build() } - fun isolateUiForPipIfNeeded(activity: android.app.Activity) { - if (context.currentActivity !== activity || autoPip != true || boundPlayer?.isPlaying != true) { + fun isolateUiForPipIfNeeded(activity: android.app.Activity, afterEnteredPip: Boolean = false) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && !afterEnteredPip) { return } + if (context.currentActivity !== activity || autoPip != true || boundPlayer == null) { + return + } + if (!afterEnteredPip && boundPlayer?.isPlaying != true) return isolateUiForPip(activity) }