diff --git a/android/src/main/java/com/bitmovin/player/reactnative/MediaSessionConnectionManager.kt b/android/src/main/java/com/bitmovin/player/reactnative/BackgroundPlaybackManager.kt similarity index 82% rename from android/src/main/java/com/bitmovin/player/reactnative/MediaSessionConnectionManager.kt rename to android/src/main/java/com/bitmovin/player/reactnative/BackgroundPlaybackManager.kt index 948e3183..9df121b8 100644 --- a/android/src/main/java/com/bitmovin/player/reactnative/MediaSessionConnectionManager.kt +++ b/android/src/main/java/com/bitmovin/player/reactnative/BackgroundPlaybackManager.kt @@ -1,6 +1,5 @@ package com.bitmovin.player.reactnative - import android.content.ComponentName import android.content.Context import android.content.Intent @@ -15,16 +14,14 @@ import com.facebook.react.bridge.* import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asStateFlow -// TODO: rename BackgroundPlaybackConnectionManager as both are form of bg playback -// TODO: mediaSessionPlayback to mediaSession -class MediaSessionConnectionManager(val context: ReactApplicationContext) { +class BackgroundPlaybackManager(val context: ReactApplicationContext) { private var isServiceStarted = false private lateinit var playerId: NativeId private val _serviceBinder = MutableStateFlow(null) val serviceBinder = _serviceBinder.asStateFlow() - inner class BackgroundPlaybackServiceConnection: ServiceConnection { + inner class BackgroundPlaybackServiceConnection : ServiceConnection { override fun onServiceConnected(className: ComponentName, service: IBinder) { val binder = service as PlayerServiceBinder _serviceBinder.value = binder @@ -37,10 +34,13 @@ class MediaSessionConnectionManager(val context: ReactApplicationContext) { } fun setupBackgroundPlayback(playerId: NativeId, playerModule: PlayerModule) { - this@MediaSessionConnectionManager.playerId = playerId - val serviceClass = if (playerModule.isMediaSessionPlaybackEnabled) - MediaSessionPlaybackService::class.java else + this@BackgroundPlaybackManager.playerId = playerId + val serviceClass = if (playerModule.isMediaSessionPlaybackEnabled) { + MediaSessionPlaybackService::class.java + } else { BackgroundPlaybackService::class.java + } +// val serviceClass = MediaSessionPlaybackService::class.java val intent = Intent(context, serviceClass) intent.action = Intent.ACTION_MEDIA_BUTTON val connection: ServiceConnection = BackgroundPlaybackServiceConnection() diff --git a/android/src/main/java/com/bitmovin/player/reactnative/PlayerModule.kt b/android/src/main/java/com/bitmovin/player/reactnative/PlayerModule.kt index 622fd5c1..95b9cb10 100644 --- a/android/src/main/java/com/bitmovin/player/reactnative/PlayerModule.kt +++ b/android/src/main/java/com/bitmovin/player/reactnative/PlayerModule.kt @@ -13,7 +13,6 @@ import com.bitmovin.player.reactnative.converter.toJson import com.bitmovin.player.reactnative.converter.toLockScreenControlConfig import com.bitmovin.player.reactnative.converter.toPlayerConfig import com.bitmovin.player.reactnative.extensions.mapToReactArray -import com.bitmovin.player.reactnative.extensions.playerModule import com.facebook.react.bridge.* import com.facebook.react.module.annotations.ReactModule import java.security.InvalidParameterException @@ -27,7 +26,7 @@ class PlayerModule(context: ReactApplicationContext) : BitmovinBaseModule(contex */ private val players: Registry = mutableMapOf() - var backgroundPlaybackConnectionManager: MediaSessionConnectionManager? = null + var backgroundPlaybackConnectionManager: BackgroundPlaybackManager? = null var isMediaSessionPlaybackEnabled: Boolean = false var isBackgroundPlaybackEnabled: Boolean = false @@ -82,9 +81,11 @@ class PlayerModule(context: ReactApplicationContext) : BitmovinBaseModule(contex val defaultMetadata = analyticsConfigJson?.getMap("defaultMetadata")?.toAnalyticsDefaultMetadata() isMediaSessionPlaybackEnabled = playerConfigJson?.getMap("lockScreenControlConfig") ?.toLockScreenControlConfig()?.isEnabled ?: false - isBackgroundPlaybackEnabled = if (isMediaSessionPlaybackEnabled) - isMediaSessionPlaybackEnabled else + isBackgroundPlaybackEnabled = if (isMediaSessionPlaybackEnabled) { + isMediaSessionPlaybackEnabled + } else { playerConfigJson?.getMap("playbackConfig")?.getBoolean("isBackgroundPlaybackEnabled") ?: false + } val networkConfig = networkNativeId?.let { networkModule.getConfig(it) } if (networkConfig != null) { @@ -103,7 +104,7 @@ class PlayerModule(context: ReactApplicationContext) : BitmovinBaseModule(contex } if (isBackgroundPlaybackEnabled) { - backgroundPlaybackConnectionManager = MediaSessionConnectionManager(context) + backgroundPlaybackConnectionManager = BackgroundPlaybackManager(context) promise.unit.resolveOnUiThread { backgroundPlaybackConnectionManager?.setupBackgroundPlayback(nativeId, this@PlayerModule) } diff --git a/android/src/main/java/com/bitmovin/player/reactnative/converter/JsonConverter.kt b/android/src/main/java/com/bitmovin/player/reactnative/converter/JsonConverter.kt index 5370e7f2..92843081 100644 --- a/android/src/main/java/com/bitmovin/player/reactnative/converter/JsonConverter.kt +++ b/android/src/main/java/com/bitmovin/player/reactnative/converter/JsonConverter.kt @@ -884,7 +884,7 @@ fun String.toMediaType(): MediaType? = when (this) { } data class LockScreenControlConfig( - var isEnabled: Boolean = false + var isEnabled: Boolean = false, ) fun ReadableMap.toLockScreenControlConfig(): LockScreenControlConfig = LockScreenControlConfig().apply { diff --git a/android/src/main/java/com/bitmovin/player/reactnative/services/MediaSessionPlaybackService.kt b/android/src/main/java/com/bitmovin/player/reactnative/services/MediaSessionPlaybackService.kt index 3feac8c1..e19378c7 100644 --- a/android/src/main/java/com/bitmovin/player/reactnative/services/MediaSessionPlaybackService.kt +++ b/android/src/main/java/com/bitmovin/player/reactnative/services/MediaSessionPlaybackService.kt @@ -20,7 +20,7 @@ class MediaSessionPlaybackService : MediaSessionService() { } fun connectSession() = mediaSession?.let { addSession(it) } - fun disconnectSession() = mediaSession?.let{ + fun disconnectSession() = mediaSession?.let { removeSession(it) it.release() }