Skip to content

Commit

Permalink
[CORE-4969] Fixed small issue with starting background service
Browse files Browse the repository at this point in the history
  • Loading branch information
KharchenkoAlex committed Sep 26, 2024
1 parent 17975b7 commit a809762
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,30 +76,6 @@ class BitmovinVideoPlayerPlugin : VideoPlayerPlugin {
}
}

DisposableEffect(currentLifecycle) {
val observer = LifecycleEventObserver { _, event ->
when (event) {
Lifecycle.Event.ON_STOP -> {
playerViewModel.handleAppInBackground(context)
}
Lifecycle.Event.ON_START -> {
playerViewModel.handleAppInForeground(context)
}
Lifecycle.Event.ON_PAUSE -> {
playerViewModel.handleAppInBackground(context)
}
Lifecycle.Event.ON_RESUME -> {
playerViewModel.handleAppInForeground(context)
}
else -> {}
}
}
currentLifecycle.lifecycle.addObserver(observer)
onDispose {
currentLifecycle.lifecycle.removeObserver(observer)
}
}

val isReady = playerViewModel.isPlayerReady.collectAsState()

key(lastHlsUrl.value) {
Expand All @@ -116,9 +92,34 @@ class BitmovinVideoPlayerPlugin : VideoPlayerPlugin {
update = { view ->
if (isReady.value) {
view.player = playerViewModel.player
playerViewModel.updateBackgroundService(context)
}
}
)

DisposableEffect(currentLifecycle) {
val observer = LifecycleEventObserver { _, event ->
when (event) {
Lifecycle.Event.ON_STOP -> {
playerViewModel.handleAppInBackground(context)
}
Lifecycle.Event.ON_START -> {
playerViewModel.handleAppInForeground(context)
}
Lifecycle.Event.ON_PAUSE -> {
playerViewModel.handleAppInBackground(context)
}
Lifecycle.Event.ON_RESUME -> {
playerViewModel.handleAppInForeground(context)
}
else -> {}
}
}
currentLifecycle.lifecycle.addObserver(observer)
onDispose {
currentLifecycle.lifecycle.removeObserver(observer)
}
}
}

if (fullscreen.value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class VideoPlayerViewModel : ViewModel() {
updateBackgroundService(context)
}

private fun updateBackgroundService(context: Context) {
fun updateBackgroundService(context: Context) {
if (backgroundPlaybackEnabled && isPermissionsGranted) {
bindToBackgroundPlaybackService(context)
}
Expand All @@ -68,7 +68,7 @@ class VideoPlayerViewModel : ViewModel() {
updateBackgroundService(context)
}

fun loadVideo(videoUrl: String) {
private fun loadVideo(videoUrl: String) {
if (!urlsAreEqualExcludingKs(currentVideoUrl ?: "", videoUrl)) {
val sourceConfig = SourceConfig.fromUrl(videoUrl)
player?.load(sourceConfig)
Expand Down Expand Up @@ -101,15 +101,15 @@ class VideoPlayerViewModel : ViewModel() {
}

fun handleAppInBackground(context: Context) {
if (backgroundPlaybackEnabled) {
if (backgroundPlaybackEnabled && _isPlayerReady.value) {
bindToBackgroundPlaybackService(context)
} else {
player?.pause()
}
}

fun handleAppInForeground(context: Context) {
if (backgroundPlaybackEnabled) {
if (backgroundPlaybackEnabled && _isPlayerReady.value) {
unbindFromService(context)
} else if (autoplayEnabled) {
player?.play()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,6 @@ class BackgroundPlaybackService : Service() {

private var player: Player? = null

fun setPlayer(context: Context) {
if (player == null) {
val playerConfig = PlayerConfig(key = PlaybackSDKManager.bitmovinLicense)
player = Player(context, playerConfig)
}
}

fun setPlayer(player: Player) {
this.player = player
}

fun releasePlayer() {
player?.destroy()
player = null
Expand Down Expand Up @@ -82,8 +71,6 @@ class BackgroundPlaybackService : Service() {
override fun onCreate() {
super.onCreate()

// setPlayer(this)

// Create a PlayerNotificationManager with the static create method
// By passing null for the mediaDescriptionAdapter, a DefaultMediaDescriptionAdapter will be used internally.
playerNotificationManager = PlayerNotificationManager.createWithNotificationChannel(
Expand All @@ -107,9 +94,6 @@ class BackgroundPlaybackService : Service() {
stopSelf()
}
})

// Attaching the Player to the PlayerNotificationManager
// setPlayer(sharedPlayer)
}
}

Expand Down

0 comments on commit a809762

Please sign in to comment.