Display player error in notification

This commit is contained in:
vfsfitvnm
2022-06-27 22:41:53 +02:00
parent 978a04bc2e
commit 8155294516

View File

@@ -100,6 +100,9 @@ class PlayerService : Service(), Player.Listener, PlaybackStatsListener.Callback
onPlaybackStateChanged(Player.STATE_READY) onPlaybackStateChanged(Player.STATE_READY)
onIsPlayingChanged(player.playWhenReady) onIsPlayingChanged(player.playWhenReady)
} }
STATE_ERROR -> {
notificationManager.notify(NotificationId, notification())
}
else -> {} else -> {}
} }
} }
@@ -267,6 +270,15 @@ class PlayerService : Service(), Player.Listener, PlaybackStatsListener.Callback
} }
} }
override fun onPlayerErrorChanged(error: PlaybackException?) {
if (error != null) {
stateBuilder
.setState(STATE_ERROR, player.currentPosition, 1f)
mediaSession.setPlaybackState(stateBuilder.build())
}
}
override fun onPositionDiscontinuity( override fun onPositionDiscontinuity(
oldPosition: Player.PositionInfo, oldPosition: Player.PositionInfo,
newPosition: Player.PositionInfo, newPosition: Player.PositionInfo,
@@ -307,11 +319,12 @@ class PlayerService : Service(), Player.Listener, PlaybackStatsListener.Callback
val builder = NotificationCompat.Builder(applicationContext, NotificationChannelId) val builder = NotificationCompat.Builder(applicationContext, NotificationChannelId)
.setContentTitle(mediaMetadata.title) .setContentTitle(mediaMetadata.title)
.setContentText(mediaMetadata.artist) .setContentText(mediaMetadata.artist)
.setSubText(player.playerError?.message)
.setLargeIcon(lastBitmap) .setLargeIcon(lastBitmap)
.setAutoCancel(true) .setAutoCancel(true)
.setOnlyAlertOnce(true) .setOnlyAlertOnce(true)
.setShowWhen(false) .setShowWhen(false)
.setSmallIcon(R.drawable.app_icon) .setSmallIcon(player.playerError?.let { R.drawable.alert_circle } ?: R.drawable.app_icon)
.setOngoing(false) .setOngoing(false)
.setContentIntent(activityPendingIntent<MainActivity>()) .setContentIntent(activityPendingIntent<MainActivity>())
.setDeleteIntent(broadCastPendingIntent<StopServiceBroadcastReceiver>()) .setDeleteIntent(broadCastPendingIntent<StopServiceBroadcastReceiver>())