Skip to content

Commit

Permalink
Cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
123mpozzi committed Oct 18, 2024
1 parent 34f63e1 commit 7fde45f
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ class MediaSessionModule(context: ReactApplicationContext) : BitmovinBaseModule(
val serviceBinder = _serviceBinder.asStateFlow()

inner class MediaSessionServiceConnection: ServiceConnection {
// we need a promise here, so that we can resolve it here instead of in setupMediaSession
// subclass ServiceConnection and pass promise
override fun onServiceConnected(className: ComponentName, service: IBinder) {
// We've bound to the Service, cast the IBinder and get the Player instance
val binder = service as MediaSessionPlaybackService.ServiceBinder
Expand All @@ -40,16 +38,8 @@ class MediaSessionModule(context: ReactApplicationContext) : BitmovinBaseModule(
}
}


// [!!!] -- on new sources, the media session does not get overridden
// ISSUE: whenever a player instance is created the service does not get
// updated/overridden with that instance.
// Because in player.ts we call `setupMediaSession` on `initialize()`.
@ReactMethod
fun setupMediaSession(playerId: NativeId) {
// if there is an existing media session session, change its content
// the change should be on play actually, not on player creation

this@MediaSessionModule.playerId = playerId
val intent = Intent(context, MediaSessionPlaybackService::class.java)
intent.action = Intent.ACTION_MEDIA_BUTTON
Expand All @@ -67,19 +57,4 @@ class MediaSessionModule(context: ReactApplicationContext) : BitmovinBaseModule(
nativeId: NativeId = playerId,
playerModule: PlayerModule? = context.playerModule,
): Player = playerModule?.getPlayerOrNull(nativeId) ?: throw IllegalArgumentException("Invalid PlayerId $nativeId")

/// FINE
// - add LockScreenConfig serializers
// - add a lock-screen control sample: only there the config is set to true
// - fix player is paused when app is minimized: see android codebase: implement Activity lfiecycle onStart and onStop and detach player
}

// ISSUE2 -- if I start playback, minimize, and restart playback. I should
// re-fetch the state of the player from the service and update
// the player in the view

// ISSUE3 -- if I start playback, minimize the app, the playback gets paused.
// It should not (it is background playback)

// ISSUE4 -- if `loadSource()` is a different source than the one playing in the
// service, load that instead.
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.bitmovin.player.reactnative

import android.content.ComponentName
import android.content.ServiceConnection
import android.os.IBinder
import android.util.Log
import com.bitmovin.analytics.api.DefaultMetadata
import com.bitmovin.player.api.Player
Expand All @@ -16,11 +13,8 @@ import com.bitmovin.player.reactnative.converter.toAnalyticsDefaultMetadata
import com.bitmovin.player.reactnative.converter.toJson
import com.bitmovin.player.reactnative.converter.toPlayerConfig
import com.bitmovin.player.reactnative.extensions.mapToReactArray
import com.bitmovin.player.reactnative.services.MediaSessionPlaybackService
import com.facebook.react.bridge.*
import com.facebook.react.module.annotations.ReactModule
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import java.security.InvalidParameterException

private const val MODULE_NAME = "PlayerModule"
Expand Down Expand Up @@ -98,23 +92,14 @@ class PlayerModule(context: ReactApplicationContext) : BitmovinBaseModule(contex
)
}

// FINAL: (ideas)
// If config is enabled, create the Intent here to start/bind the service -- setupMediaSession()
// Put the nativeId in the thread

if (playerConfig.lockScreenControlConfig.isEnabled) {
// playerConfig.playbackConfig
promise.unit.resolveOnUiThread {
mediaSessionModule
.setupMediaSession(nativeId)
}
}
}

// FINAL: 2 issues to solve:
// - where is the player created
// - how the player instance is given to the service (binder setting player vs playerId passing)

/**
* Load the source of the given [nativeId] with `config` options from JS.
* @param nativeId Target player.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ 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 @@ -117,13 +116,10 @@ class RNPlayerView(
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) {
if (mediaSessionServicePlayer != null) {
oldPlayer = player
player = mediaSessionServicePlayer
}
playerView?.onStart()
Expand All @@ -140,18 +136,10 @@ class RNPlayerView(
override fun onStop(owner: LifecycleOwner) {
if (player?.config?.lockScreenControlConfig?.isEnabled == false) {
mediaSessionServicePlayer = null
}
else {
} 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 7fde45f

Please sign in to comment.