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

Fix/Android: crash in Media Service #325

Closed
Changes from all commits
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
22 changes: 10 additions & 12 deletions android/src/main/java/com/theoplayer/media/MediaPlaybackService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class MediaPlaybackService : MediaBrowserServiceCompat() {
super.onTaskRemoved(rootIntent)
if (STOP_SERVICE_IF_APP_REMOVED) {
notificationManager.cancel(NOTIFICATION_ID)
stopSelf()
stopForegroundService()
}
}

Expand Down Expand Up @@ -279,6 +279,8 @@ class MediaPlaybackService : MediaBrowserServiceCompat() {
}
}
PlaybackStateCompat.STATE_STOPPED -> {
// Fixes the crash where the service is stopped
startForegroundWithPlaybackState(playbackState, largeIcon = loadPlaceHolderIcon(this))
// Remove this service from foreground state, allowing it to be killed if more memory is
// needed. This does not stop the service from running (for that you use stopSelf()
// or related methods), just takes it out of the foreground state.
Expand All @@ -293,18 +295,14 @@ class MediaPlaybackService : MediaBrowserServiceCompat() {

private fun startForegroundWithPlaybackState(@PlaybackStateCompat.State playbackState: Int, largeIcon: Bitmap? = null) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
startForeground(
NOTIFICATION_ID,
notificationBuilder.build(playbackState, largeIcon, enableMediaControls),
ServiceCompat.startForeground(
this,
NOTIFICATION_ID,
notificationBuilder.build(playbackState, largeIcon, enableMediaControls),
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
)
} else {
startForeground(
NOTIFICATION_ID,
notificationBuilder.build(playbackState, largeIcon, enableMediaControls)
)
}
else 0
)
} catch (e: IllegalStateException) {
// Make sure that app does not crash in case anything goes wrong with starting the service.
// https://issuetracker.google.com/issues/229000935
Expand Down
Loading