toast a friendly message for playback connection errors

Map the Media3 network/HTTP error codes to readable text and show it as a toast, instead of leaving an opaque ERR_ code on the now playing screen.
This commit is contained in:
austinried
2026-10-04 00:29:59 +00:00
parent 258dde6c86
commit 7fb832e116
5 changed files with 43 additions and 4 deletions
@@ -45,7 +45,7 @@ fun appModule(
single { SyncManager(get(), get(), get()) }
single { QueueRepository(get()) }
single<PlayerConnection> { MediaSessionConnection(context.applicationContext, get()) }
single { PlaybackController(get(), get(), get()) }
single { PlaybackController(get(), get(), get(), showMessage = toast) }
viewModel { RootViewModel(get()) }
viewModel { LibraryViewModel(get(), get(), get(), get(), get()) }
viewModel { SettingsViewModel(get(), get()) }
@@ -111,7 +111,7 @@ class Media3PlayerHandle(
"Playback error ${error.errorCodeName} at ${controller.currentPosition}/${controller.duration}",
error,
)
listener.onError(error.errorCodeName)
listener.onError(playbackErrorMessage(error))
}
override fun onPlaybackStateChanged(playbackState: Int) {
@@ -171,3 +171,14 @@ class Media3PlayerHandle(
const val EXTRA_DURATION_MS = "durationMs"
}
}
internal fun playbackErrorMessage(error: PlaybackException): String =
when (error.errorCode) {
PlaybackException.ERROR_CODE_IO_NETWORK_CONNECTION_FAILED,
PlaybackException.ERROR_CODE_IO_NETWORK_CONNECTION_TIMEOUT,
-> "Can't reach the server. Check your connection."
PlaybackException.ERROR_CODE_IO_BAD_HTTP_STATUS -> "The server refused to stream this track."
else -> error.errorCodeName
}
@@ -98,6 +98,7 @@ class PlaybackController(
private val sourceRepository: SourceRepository,
private val queueRepository: QueueRepository,
private val connection: PlayerConnection,
private val showMessage: (String) -> Unit = {},
dispatcher: CoroutineDispatcher = Dispatchers.Main.immediate,
) {
private val scope = CoroutineScope(SupervisorJob() + dispatcher)
@@ -870,6 +871,7 @@ class PlaybackController(
override fun onError(message: String) {
lastError = message
showMessage(message)
refresh()
}
}