Fix service startup bug
Fix a problem where if playback was started by a file intent the ExoPlayer instance wouldn't actually begin playing.
This commit is contained in:
parent
af9fac7de2
commit
891d64b459
3 changed files with 39 additions and 16 deletions
|
@ -181,6 +181,8 @@ class PlaybackViewModel : ViewModel(), PlaybackStateManager.Callback {
|
|||
* This is called after [playWithUri] once its deemed safe to do so.
|
||||
*/
|
||||
private fun playWithUriInternal(uri: Uri, context: Context) {
|
||||
logD("Playing with uri $uri")
|
||||
|
||||
musicStore.getSongForUri(uri, context.contentResolver)?.let { song ->
|
||||
playSong(song)
|
||||
}
|
||||
|
@ -373,12 +375,12 @@ class PlaybackViewModel : ViewModel(), PlaybackStateManager.Callback {
|
|||
val intentUri = mIntentUri
|
||||
|
||||
if (intentUri != null) {
|
||||
// Were not going to be restoring playbackManager after this, so mark it as such.
|
||||
playbackManager.setRestored()
|
||||
playWithUriInternal(intentUri, context)
|
||||
|
||||
// Remove the uri after finishing the calls so that this does not fire again.
|
||||
mIntentUri = null
|
||||
|
||||
// Were not going to be restoring playbackManager after this, so mark it as such.
|
||||
playbackManager.setRestored()
|
||||
} else if (!playbackManager.isRestored) {
|
||||
// Otherwise just restore
|
||||
viewModelScope.launch {
|
||||
|
|
|
@ -575,10 +575,10 @@ class PlaybackStateManager private constructor() {
|
|||
}
|
||||
|
||||
/**
|
||||
* Reset the has played status as if this instance is fresh.
|
||||
* Mark whether this instance has played or not
|
||||
*/
|
||||
fun resetHasPlayedStatus() {
|
||||
mHasPlayed = false
|
||||
fun setHasPlayed(hasPlayed: Boolean) {
|
||||
mHasPlayed = hasPlayed
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -140,7 +140,7 @@ class PlaybackService : Service(), Player.EventListener, PlaybackStateManager.Ca
|
|||
|
||||
// --- PLAYBACKSTATEMANAGER SETUP ---
|
||||
|
||||
playbackManager.resetHasPlayedStatus()
|
||||
playbackManager.setHasPlayed(playbackManager.isPlaying)
|
||||
playbackManager.addCallback(this)
|
||||
|
||||
if (playbackManager.song != null || playbackManager.isRestored) {
|
||||
|
@ -150,6 +150,8 @@ class PlaybackService : Service(), Player.EventListener, PlaybackStateManager.Ca
|
|||
// --- SETTINGSMANAGER SETUP ---
|
||||
|
||||
settingsManager.addCallback(this)
|
||||
|
||||
logD("Service created.")
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
|
@ -165,6 +167,9 @@ class PlaybackService : Service(), Player.EventListener, PlaybackStateManager.Ca
|
|||
playbackManager.removeCallback(this)
|
||||
settingsManager.removeCallback(this)
|
||||
|
||||
// Pause just in case this destruction was unexpected.
|
||||
playbackManager.setPlaying(false)
|
||||
|
||||
// The service coroutines last job is to save the state to the DB, before terminating itself
|
||||
serviceScope.launch {
|
||||
playbackManager.saveStateToDatabase(this@PlaybackService)
|
||||
|
@ -212,9 +217,11 @@ class PlaybackService : Service(), Player.EventListener, PlaybackStateManager.Ca
|
|||
pushMetadataToSession(song)
|
||||
|
||||
notification.setMetadata(
|
||||
this, song, settingsManager.colorizeNotif, { startForegroundOrNotify() }
|
||||
this, song, settingsManager.colorizeNotif, ::startForegroundOrNotify
|
||||
)
|
||||
|
||||
logD("Song Status: $song")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -240,6 +247,8 @@ class PlaybackService : Service(), Player.EventListener, PlaybackStateManager.Ca
|
|||
|
||||
notification.setPlaying(this, isPlaying)
|
||||
startForegroundOrNotify()
|
||||
|
||||
logD("Playing Status: $isPlaying")
|
||||
}
|
||||
|
||||
override fun onLoopUpdate(loopMode: LoopMode) {
|
||||
|
@ -287,7 +296,7 @@ class PlaybackService : Service(), Player.EventListener, PlaybackStateManager.Ca
|
|||
override fun onShowCoverUpdate(showCovers: Boolean) {
|
||||
playbackManager.song?.let { song ->
|
||||
notification.setMetadata(
|
||||
this, song, settingsManager.colorizeNotif, { startForegroundOrNotify() }
|
||||
this, song, settingsManager.colorizeNotif, ::startForegroundOrNotify
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -295,7 +304,7 @@ class PlaybackService : Service(), Player.EventListener, PlaybackStateManager.Ca
|
|||
override fun onQualityCoverUpdate(doQualityCovers: Boolean) {
|
||||
playbackManager.song?.let { song ->
|
||||
notification.setMetadata(
|
||||
this, song, settingsManager.colorizeNotif, { startForegroundOrNotify() }
|
||||
this, song, settingsManager.colorizeNotif, ::startForegroundOrNotify
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -326,14 +335,18 @@ class PlaybackService : Service(), Player.EventListener, PlaybackStateManager.Ca
|
|||
* Fully restore the notification and playback state
|
||||
*/
|
||||
private fun restore() {
|
||||
playbackManager.song?.let { song ->
|
||||
notification.setMetadata(this, song, settingsManager.colorizeNotif) {}
|
||||
logD("Restoring the service state")
|
||||
|
||||
player.setMediaItem(MediaItem.fromUri(song.id.toURI()))
|
||||
player.seekTo(playbackManager.position)
|
||||
player.prepare()
|
||||
}
|
||||
// Re-call existing callbacks with the current values to restore everything
|
||||
onParentUpdate(playbackManager.parent)
|
||||
onPlayingUpdate(playbackManager.isPlaying)
|
||||
onShuffleUpdate(playbackManager.isShuffling)
|
||||
onLoopUpdate(playbackManager.loopMode)
|
||||
onSongUpdate(playbackManager.song)
|
||||
onSeek(playbackManager.position)
|
||||
|
||||
/*
|
||||
Old Manual restore code, restore this if the above causes bugs
|
||||
notification.setParent(this, playbackManager.parent)
|
||||
notification.setPlaying(this, playbackManager.isPlaying)
|
||||
|
||||
|
@ -344,6 +357,14 @@ class PlaybackService : Service(), Player.EventListener, PlaybackStateManager.Ca
|
|||
}
|
||||
|
||||
player.setLoopMode(playbackManager.loopMode)
|
||||
|
||||
playbackManager.song?.let { song ->
|
||||
notification.setMetadata(this, song, settingsManager.colorizeNotif) {}
|
||||
|
||||
player.setMediaItem(MediaItem.fromUri(song.id.toURI()))
|
||||
player.seekTo(playbackManager.position)
|
||||
player.prepare()
|
||||
}*/
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue