From 34f63e17994e2615b577d86499ec9c02e12bbc9f Mon Sep 17 00:00:00 2001 From: Michele Pozzi <123.mpozzi@gmail.com> Date: Fri, 18 Oct 2024 10:28:15 +0200 Subject: [PATCH] Fix every sample having background playback after service is on --- .../player/reactnative/MediaSessionModule.kt | 5 +--- .../player/reactnative/RNPlayerView.kt | 29 ++++++++++++++++--- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/android/src/main/java/com/bitmovin/player/reactnative/MediaSessionModule.kt b/android/src/main/java/com/bitmovin/player/reactnative/MediaSessionModule.kt index 8ef00a5a..a3628093 100644 --- a/android/src/main/java/com/bitmovin/player/reactnative/MediaSessionModule.kt +++ b/android/src/main/java/com/bitmovin/player/reactnative/MediaSessionModule.kt @@ -22,9 +22,6 @@ class MediaSessionModule(context: ReactApplicationContext) : BitmovinBaseModule( private var isServiceStarted = false private lateinit var playerId: NativeId - private val _player = MutableStateFlow(null) - val player = _player.asStateFlow() - private val _serviceBinder = MutableStateFlow(null) val serviceBinder = _serviceBinder.asStateFlow() @@ -39,7 +36,7 @@ class MediaSessionModule(context: ReactApplicationContext) : BitmovinBaseModule( } override fun onServiceDisconnected(name: ComponentName) { - _player.value = null + _serviceBinder.value?.player = null } } diff --git a/android/src/main/java/com/bitmovin/player/reactnative/RNPlayerView.kt b/android/src/main/java/com/bitmovin/player/reactnative/RNPlayerView.kt index 2481fca8..c216aef2 100644 --- a/android/src/main/java/com/bitmovin/player/reactnative/RNPlayerView.kt +++ b/android/src/main/java/com/bitmovin/player/reactnative/RNPlayerView.kt @@ -15,8 +15,10 @@ import com.bitmovin.player.api.event.PlayerEvent import com.bitmovin.player.api.event.SourceEvent import com.bitmovin.player.api.ui.PlayerViewConfig import com.bitmovin.player.api.ui.StyleConfig +import com.bitmovin.player.reactnative.converter.lockScreenControlConfig import com.bitmovin.player.reactnative.converter.toJson import com.bitmovin.player.reactnative.extensions.mediaSessionModule +import com.bitmovin.player.reactnative.extensions.playerModule import com.facebook.react.ReactActivity import com.facebook.react.bridge.* import com.facebook.react.uimanager.events.RCTEventEmitter @@ -109,12 +111,20 @@ class RNPlayerView( */ private var playerEventRelay: EventRelay + private var mediaSessionServicePlayer: Player? + get() = context.mediaSessionModule?.serviceBinder?.value?.player + set(value) { + context.mediaSessionModule?.serviceBinder?.value?.player = value + } + + private var oldPlayer: Player? = null + private val activityLifecycleObserver = object : DefaultLifecycleObserver { // Don't stop the player when going to background override fun onStart(owner: LifecycleOwner) { -// playerView?.player = context.mediaSessionModule?.serviceBinder?.value?.player - if (context.mediaSessionModule?.serviceBinder?.value?.player != null) { - player = context.mediaSessionModule?.serviceBinder?.value?.player + if (mediaSessionServicePlayer != null) { + oldPlayer = player + player = mediaSessionServicePlayer } playerView?.onStart() } @@ -128,9 +138,20 @@ class RNPlayerView( } override fun onStop(owner: LifecycleOwner) { - if (context.mediaSessionModule?.serviceBinder?.value?.player != null) { + if (player?.config?.lockScreenControlConfig?.isEnabled == false) { + mediaSessionServicePlayer = null + } + else { player = null } +// if (mediaSessionServicePlayer != null) { +//// context.mediaSessionModule?.serviceBinder?.value?.player = null +//// if (mediaSessionServicePlayer != player) { +//// mediaSessionServicePlayer = null +//// } +// +// player = oldPlayer +// } playerView?.onStop() }