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.
|
* This is called after [playWithUri] once its deemed safe to do so.
|
||||||
*/
|
*/
|
||||||
private fun playWithUriInternal(uri: Uri, context: Context) {
|
private fun playWithUriInternal(uri: Uri, context: Context) {
|
||||||
|
logD("Playing with uri $uri")
|
||||||
|
|
||||||
musicStore.getSongForUri(uri, context.contentResolver)?.let { song ->
|
musicStore.getSongForUri(uri, context.contentResolver)?.let { song ->
|
||||||
playSong(song)
|
playSong(song)
|
||||||
}
|
}
|
||||||
|
@ -373,12 +375,12 @@ class PlaybackViewModel : ViewModel(), PlaybackStateManager.Callback {
|
||||||
val intentUri = mIntentUri
|
val intentUri = mIntentUri
|
||||||
|
|
||||||
if (intentUri != null) {
|
if (intentUri != null) {
|
||||||
// Were not going to be restoring playbackManager after this, so mark it as such.
|
|
||||||
playbackManager.setRestored()
|
|
||||||
playWithUriInternal(intentUri, context)
|
playWithUriInternal(intentUri, context)
|
||||||
|
|
||||||
// Remove the uri after finishing the calls so that this does not fire again.
|
// Remove the uri after finishing the calls so that this does not fire again.
|
||||||
mIntentUri = null
|
mIntentUri = null
|
||||||
|
|
||||||
|
// Were not going to be restoring playbackManager after this, so mark it as such.
|
||||||
|
playbackManager.setRestored()
|
||||||
} else if (!playbackManager.isRestored) {
|
} else if (!playbackManager.isRestored) {
|
||||||
// Otherwise just restore
|
// Otherwise just restore
|
||||||
viewModelScope.launch {
|
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() {
|
fun setHasPlayed(hasPlayed: Boolean) {
|
||||||
mHasPlayed = false
|
mHasPlayed = hasPlayed
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -140,7 +140,7 @@ class PlaybackService : Service(), Player.EventListener, PlaybackStateManager.Ca
|
||||||
|
|
||||||
// --- PLAYBACKSTATEMANAGER SETUP ---
|
// --- PLAYBACKSTATEMANAGER SETUP ---
|
||||||
|
|
||||||
playbackManager.resetHasPlayedStatus()
|
playbackManager.setHasPlayed(playbackManager.isPlaying)
|
||||||
playbackManager.addCallback(this)
|
playbackManager.addCallback(this)
|
||||||
|
|
||||||
if (playbackManager.song != null || playbackManager.isRestored) {
|
if (playbackManager.song != null || playbackManager.isRestored) {
|
||||||
|
@ -150,6 +150,8 @@ class PlaybackService : Service(), Player.EventListener, PlaybackStateManager.Ca
|
||||||
// --- SETTINGSMANAGER SETUP ---
|
// --- SETTINGSMANAGER SETUP ---
|
||||||
|
|
||||||
settingsManager.addCallback(this)
|
settingsManager.addCallback(this)
|
||||||
|
|
||||||
|
logD("Service created.")
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
|
@ -165,6 +167,9 @@ class PlaybackService : Service(), Player.EventListener, PlaybackStateManager.Ca
|
||||||
playbackManager.removeCallback(this)
|
playbackManager.removeCallback(this)
|
||||||
settingsManager.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
|
// The service coroutines last job is to save the state to the DB, before terminating itself
|
||||||
serviceScope.launch {
|
serviceScope.launch {
|
||||||
playbackManager.saveStateToDatabase(this@PlaybackService)
|
playbackManager.saveStateToDatabase(this@PlaybackService)
|
||||||
|
@ -212,9 +217,11 @@ class PlaybackService : Service(), Player.EventListener, PlaybackStateManager.Ca
|
||||||
pushMetadataToSession(song)
|
pushMetadataToSession(song)
|
||||||
|
|
||||||
notification.setMetadata(
|
notification.setMetadata(
|
||||||
this, song, settingsManager.colorizeNotif, { startForegroundOrNotify() }
|
this, song, settingsManager.colorizeNotif, ::startForegroundOrNotify
|
||||||
)
|
)
|
||||||
|
|
||||||
|
logD("Song Status: $song")
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -240,6 +247,8 @@ class PlaybackService : Service(), Player.EventListener, PlaybackStateManager.Ca
|
||||||
|
|
||||||
notification.setPlaying(this, isPlaying)
|
notification.setPlaying(this, isPlaying)
|
||||||
startForegroundOrNotify()
|
startForegroundOrNotify()
|
||||||
|
|
||||||
|
logD("Playing Status: $isPlaying")
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onLoopUpdate(loopMode: LoopMode) {
|
override fun onLoopUpdate(loopMode: LoopMode) {
|
||||||
|
@ -287,7 +296,7 @@ class PlaybackService : Service(), Player.EventListener, PlaybackStateManager.Ca
|
||||||
override fun onShowCoverUpdate(showCovers: Boolean) {
|
override fun onShowCoverUpdate(showCovers: Boolean) {
|
||||||
playbackManager.song?.let { song ->
|
playbackManager.song?.let { song ->
|
||||||
notification.setMetadata(
|
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) {
|
override fun onQualityCoverUpdate(doQualityCovers: Boolean) {
|
||||||
playbackManager.song?.let { song ->
|
playbackManager.song?.let { song ->
|
||||||
notification.setMetadata(
|
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
|
* Fully restore the notification and playback state
|
||||||
*/
|
*/
|
||||||
private fun restore() {
|
private fun restore() {
|
||||||
playbackManager.song?.let { song ->
|
logD("Restoring the service state")
|
||||||
notification.setMetadata(this, song, settingsManager.colorizeNotif) {}
|
|
||||||
|
|
||||||
player.setMediaItem(MediaItem.fromUri(song.id.toURI()))
|
// Re-call existing callbacks with the current values to restore everything
|
||||||
player.seekTo(playbackManager.position)
|
onParentUpdate(playbackManager.parent)
|
||||||
player.prepare()
|
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.setParent(this, playbackManager.parent)
|
||||||
notification.setPlaying(this, playbackManager.isPlaying)
|
notification.setPlaying(this, playbackManager.isPlaying)
|
||||||
|
|
||||||
|
@ -344,6 +357,14 @@ class PlaybackService : Service(), Player.EventListener, PlaybackStateManager.Ca
|
||||||
}
|
}
|
||||||
|
|
||||||
player.setLoopMode(playbackManager.loopMode)
|
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