Skip to content

Commit

Permalink
[ZEUS-4787] Fixed background service issue when switching video url
Browse files Browse the repository at this point in the history
  • Loading branch information
KharchenkoAlex committed Aug 19, 2024
1 parent bf5a802 commit 1b939d5
Showing 1 changed file with 22 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.streamamg.player.plugin.bitmovin

import android.Manifest
import android.app.Activity
import android.app.ActivityManager
import android.content.ComponentName
import android.content.Context
import android.content.ContextWrapper
Expand Down Expand Up @@ -82,17 +83,12 @@ class BitmovinVideoPlayerPlugin : VideoPlayerPlugin {
AndroidView(
modifier = Modifier.fillMaxSize(),
factory = { context ->

if (!playerConfig.playbackConfig.backgroundPlaybackEnabled) {
// Init the Player without the Background service
if (playerBind == null) {
val playerConfig =
PlayerConfig(key = PlaybackSDKManager.bitmovinLicense)
playerBind = Player(context, playerConfig)
}
if (playerView == null) {
playerView = PlayerView(context, playerBind)
}
val playerConfig =
PlayerConfig(key = PlaybackSDKManager.bitmovinLicense)
playerBind = Player(context, playerConfig)
playerView = PlayerView(context, playerBind)
initializePlayer(lastHlsUrl.value)
} else {
// Init the Player with the Background service later in the BackgroundPlaybackService
Expand Down Expand Up @@ -223,11 +219,24 @@ class BitmovinVideoPlayerPlugin : VideoPlayerPlugin {
}
}

fun isServiceRunning(context: Context, serviceClass: Class<*>): Boolean {
val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
val services: List<ActivityManager.RunningServiceInfo> = activityManager.getRunningServices(Int.MAX_VALUE)

for (runningServiceInfo in services) {
if (runningServiceInfo.service.getClassName().equals(serviceClass.name)) {
return true
}
}
return false
}

private fun bindAndStartBackgroundService(context: Context) {
val intent = Intent(context, BackgroundPlaybackService::class.java)

try {
if (BackgroundPlaybackService.isRunning) {
if (isServiceRunning(context, BackgroundPlaybackService::class.java)) {
context.unbindService(mConnection)
context.stopService(intent)
}
context.bindService(intent, mConnection, Context.BIND_AUTO_CREATE)
Expand All @@ -238,7 +247,9 @@ class BitmovinVideoPlayerPlugin : VideoPlayerPlugin {
}

private fun unbindAndStopBackgroundService(context: Context) {
if (!isServiceBound) return
if (!isServiceBound) {
return
}

val intent = Intent(context, BackgroundPlaybackService::class.java)

Expand Down

0 comments on commit 1b939d5

Please sign in to comment.