Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ZEUS-4778] Prevent crash caused by double unbinding of background service #23

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class BitmovinVideoPlayerPlugin : VideoPlayerPlugin {
private var playerConfig = VideoPlayerConfig()
private var playerBind: Player? = null
private val fullscreen = mutableStateOf(false)
private var isServiceBound = false

override fun setup(config: VideoPlayerConfig) {
playerConfig.playbackConfig.autoplayEnabled = config.playbackConfig.autoplayEnabled
Expand All @@ -72,7 +73,7 @@ class BitmovinVideoPlayerPlugin : VideoPlayerPlugin {
RequestMissingPermissions()
} else {
// Bind and start the Background service without permissions
backgroundService(true, LocalContext.current)
bindAndStartBackgroundService(LocalContext.current)
}
}

Expand Down Expand Up @@ -127,8 +128,7 @@ class BitmovinVideoPlayerPlugin : VideoPlayerPlugin {
}
override fun onDestroy(owner: LifecycleOwner) {
if (playerConfig.playbackConfig.backgroundPlaybackEnabled) {
// Stop and unbind the Background service
backgroundService(false, context)
unbindAndStopBackgroundService(context)
}
}
}
Expand Down Expand Up @@ -210,7 +210,7 @@ class BitmovinVideoPlayerPlugin : VideoPlayerPlugin {
val context = LocalContext.current
val permissionState = rememberPermissionState(permission = Manifest.permission.POST_NOTIFICATIONS) { granted ->
if (granted) {
backgroundService(true, context)
bindAndStartBackgroundService(context)
}
}
if (!permissionState.status.isGranted) {
Expand All @@ -219,29 +219,34 @@ class BitmovinVideoPlayerPlugin : VideoPlayerPlugin {
block = { permissionState.launchPermissionRequest() }
)
} else {
// Bind and start the Background service
backgroundService(true, context)
bindAndStartBackgroundService(context)
}
}

private fun backgroundService(start: Boolean, context: Context) {
private fun bindAndStartBackgroundService(context: Context) {
val intent = Intent(context, BackgroundPlaybackService::class.java)
if (start) {
if (BackgroundPlaybackService.isRunning) {
context.stopService(intent)
}
context.bindService(intent, mConnection, Context.BIND_AUTO_CREATE)
context.startService(intent)
} else {
if (BackgroundPlaybackService.isRunning) {
saberouechtati marked this conversation as resolved.
Show resolved Hide resolved
saberouechtati marked this conversation as resolved.
Show resolved Hide resolved
context.stopService(intent)
context.unbindService(mConnection)
}
context.bindService(intent, mConnection, Context.BIND_AUTO_CREATE)
context.startService(intent)
}

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

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

context.unbindService(mConnection)
context.stopService(intent)
isServiceBound = false
}

/**
* Defines callbacks for service binding, passed to bindService()
*/
private val mConnection = object : ServiceConnection {

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 BackgroundPlaybackService.BackgroundBinder
Expand All @@ -254,9 +259,11 @@ class BitmovinVideoPlayerPlugin : VideoPlayerPlugin {
playerView?.player = playerBind

initializePlayer([email protected])
isServiceBound = true
}

override fun onServiceDisconnected(arg0: ComponentName) {
isServiceBound = false
}
}

Expand Down
Loading