Skip to content

Commit

Permalink
Fix every sample having background playback after service is on
Browse files Browse the repository at this point in the history
  • Loading branch information
123mpozzi committed Oct 18, 2024
1 parent 904d8c0 commit 34f63e1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ class MediaSessionModule(context: ReactApplicationContext) : BitmovinBaseModule(
private var isServiceStarted = false
private lateinit var playerId: NativeId

private val _player = MutableStateFlow<Player?>(null)
val player = _player.asStateFlow()

private val _serviceBinder = MutableStateFlow<MediaSessionPlaybackService.ServiceBinder?>(null)
val serviceBinder = _serviceBinder.asStateFlow()

Expand All @@ -39,7 +36,7 @@ class MediaSessionModule(context: ReactApplicationContext) : BitmovinBaseModule(
}

override fun onServiceDisconnected(name: ComponentName) {
_player.value = null
_serviceBinder.value?.player = null
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -109,12 +111,20 @@ class RNPlayerView(
*/
private var playerEventRelay: EventRelay<Player, Event>

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()
}
Expand All @@ -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()
}

Expand Down

0 comments on commit 34f63e1

Please sign in to comment.