-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #554 from bitmovin/feature/base-enable-lock-screen…
…-controls Support Media Controls
- Loading branch information
Showing
17 changed files
with
384 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
android/src/main/java/com/bitmovin/player/reactnative/MediaSessionPlaybackManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.bitmovin.player.reactnative | ||
|
||
import android.content.ComponentName | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.content.ServiceConnection | ||
import android.os.IBinder | ||
import com.bitmovin.player.api.Player | ||
import com.bitmovin.player.reactnative.extensions.playerModule | ||
import com.bitmovin.player.reactnative.services.MediaSessionPlaybackService | ||
import com.facebook.react.bridge.* | ||
|
||
class MediaSessionPlaybackManager(val context: ReactApplicationContext) { | ||
private var serviceBinder: MediaSessionPlaybackService.ServiceBinder? = null | ||
private lateinit var playerId: NativeId | ||
val player: Player? | ||
get() = serviceBinder?.player | ||
|
||
inner class MediaSessionPlaybackServiceConnection : ServiceConnection { | ||
override fun onServiceConnected(className: ComponentName, service: IBinder) { | ||
val binder = service as MediaSessionPlaybackService.ServiceBinder | ||
serviceBinder = binder | ||
binder.player = getPlayer() | ||
} | ||
|
||
override fun onServiceDisconnected(name: ComponentName) { | ||
destroy(playerId) | ||
} | ||
} | ||
|
||
fun setupMediaSessionPlayback(playerId: NativeId) { | ||
this.playerId = playerId | ||
|
||
val intent = Intent(context, MediaSessionPlaybackService::class.java) | ||
intent.action = Intent.ACTION_MEDIA_BUTTON | ||
val connection: ServiceConnection = MediaSessionPlaybackServiceConnection() | ||
context.bindService(intent, connection, Context.BIND_AUTO_CREATE) | ||
} | ||
|
||
fun destroy(nativeId: NativeId) { | ||
if (nativeId != playerId) { return } | ||
serviceBinder?.player = null | ||
serviceBinder = null | ||
} | ||
|
||
private fun getPlayer( | ||
nativeId: NativeId = playerId, | ||
playerModule: PlayerModule? = context.playerModule, | ||
): Player = playerModule?.getPlayerOrNull(nativeId) ?: throw IllegalArgumentException("Invalid PlayerId $nativeId") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...oid/src/main/java/com/bitmovin/player/reactnative/services/MediaSessionPlaybackService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.bitmovin.player.reactnative.services | ||
|
||
import android.content.Intent | ||
import android.os.Binder | ||
import android.os.IBinder | ||
import com.bitmovin.player.api.Player | ||
import com.bitmovin.player.api.media.session.MediaSession | ||
import com.bitmovin.player.api.media.session.MediaSessionService | ||
|
||
class MediaSessionPlaybackService : MediaSessionService() { | ||
inner class ServiceBinder : Binder() { | ||
var player: Player? | ||
get() = this@MediaSessionPlaybackService.player | ||
set(value) { | ||
if (player == value) { | ||
return | ||
} | ||
|
||
disconnectSession() | ||
this@MediaSessionPlaybackService.player = value | ||
value?.let { | ||
createSession(it) | ||
connectSession() | ||
} | ||
} | ||
} | ||
|
||
private var player: Player? = null | ||
private val binder = ServiceBinder() | ||
private var mediaSession: MediaSession? = null | ||
|
||
override fun onGetSession(): MediaSession? = null | ||
|
||
override fun onDestroy() { | ||
disconnectSession() | ||
player = null | ||
|
||
super.onDestroy() | ||
} | ||
|
||
override fun onBind(intent: Intent?): IBinder { | ||
super.onBind(intent) | ||
return binder | ||
} | ||
|
||
private fun createSession(player: Player) { | ||
mediaSession = MediaSession( | ||
this, | ||
mainLooper, | ||
player, | ||
) | ||
} | ||
|
||
private fun connectSession() = mediaSession?.let { addSession(it) } | ||
private fun disconnectSession() = mediaSession?.let { | ||
removeSession(it) | ||
it.release() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.