Skip to content

Commit

Permalink
Merge pull request #1946 from PaulWoitaschek/session_crash_fixes
Browse files Browse the repository at this point in the history
Session crash fixes
  • Loading branch information
PaulWoitaschek authored May 28, 2023
2 parents f580526 + 5718f07 commit f9cb8c4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
14 changes: 11 additions & 3 deletions playback/src/main/kotlin/voice/playback/PlayerController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import voice.common.BookId
import voice.common.pref.CurrentBook
import voice.data.ChapterId
import voice.data.repo.BookRepository
import voice.logging.core.Logger
import voice.playback.misc.Decibel
import voice.playback.session.CustomCommand
import voice.playback.session.MediaId
Expand Down Expand Up @@ -66,7 +67,7 @@ class PlayerController

fun pauseIfCurrentBookDifferentFrom(id: BookId) {
scope.launch {
val controller = awaitConnect()
val controller = awaitConnect() ?: return@launch
val currentBookId = controller.currentBookId()
if (currentBookId != null && currentBookId != id) {
controller.pause()
Expand Down Expand Up @@ -150,12 +151,19 @@ class PlayerController

private inline fun executeAfterPrepare(crossinline action: suspend (MediaController) -> Unit) {
scope.launch {
val controller = controller.await()
val controller = awaitConnect() ?: return@launch
if (maybePrepare(controller)) {
action(controller)
}
}
}

suspend fun awaitConnect(): MediaController = controller.await()
suspend fun awaitConnect(): MediaController? {
return try {
controller.await()
} catch (e: Exception) {
Logger.e(e, "Error while connecting to media controller")
null
}
}
}
10 changes: 10 additions & 0 deletions playback/src/main/kotlin/voice/playback/session/PlaybackService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.media3.session.MediaSession
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.cancel
import voice.common.rootComponentAs
import voice.logging.core.Logger
import voice.playback.di.PlaybackComponent
import voice.playback.player.VoicePlayer
import javax.inject.Inject
Expand Down Expand Up @@ -33,6 +34,15 @@ class PlaybackService : MediaLibraryService() {
setMediaNotificationProvider(voiceNotificationProvider)
}

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
return try {
super.onStartCommand(intent, flags, startId)
} catch (e: SecurityException) {
Logger.e(e, "onStartCommand crashed")
START_STICKY
}
}

override fun onDestroy() {
super.onDestroy()
release()
Expand Down

0 comments on commit f9cb8c4

Please sign in to comment.