Restore position properly on playback resumption
This commit is contained in:
parent
105d127a0b
commit
c5cb68a3b3
2 changed files with 32 additions and 12 deletions
|
@ -17,6 +17,9 @@ interface QueueItemsDao {
|
|||
@Query("UPDATE queue_items SET is_current = 0")
|
||||
fun resetCurrent()
|
||||
|
||||
@Query("SELECT * FROM queue_items WHERE is_current = 1")
|
||||
fun getCurrent(): QueueItem?
|
||||
|
||||
@Query("UPDATE queue_items SET is_current = 1, last_position = :lastPosition WHERE track_id = :trackId")
|
||||
fun saveCurrentTrack(trackId: Long, lastPosition: Int)
|
||||
|
||||
|
|
|
@ -244,9 +244,16 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr
|
|||
} else {
|
||||
mTracks = getQueuedTracks()
|
||||
|
||||
val wantedTrackId = intent?.getLongExtra(TRACK_ID, -1L)
|
||||
var wantedTrackId = intent?.getLongExtra(TRACK_ID, -1L) ?: -1L
|
||||
if (wantedTrackId == -1L) {
|
||||
val currentQueueItem = queueDAO.getCurrent()
|
||||
if (currentQueueItem != null) {
|
||||
wantedTrackId = currentQueueItem.trackId
|
||||
mSetProgressOnPrepare = currentQueueItem.lastPosition
|
||||
}
|
||||
}
|
||||
|
||||
// use an oldschool loop to avoid ConcurrentModificationException
|
||||
// use an old school loop to avoid ConcurrentModificationException
|
||||
for (i in 0 until mTracks.size) {
|
||||
val track = mTracks[i]
|
||||
if (track.mediaStoreId == wantedTrackId) {
|
||||
|
@ -269,7 +276,7 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr
|
|||
ensureBackgroundThread {
|
||||
initService(intent)
|
||||
|
||||
val wantedTrackId = intent?.getLongExtra(TRACK_ID, -1L) ?: -1L
|
||||
val wantedTrackId = mCurrTrack?.mediaStoreId ?: -1L
|
||||
mPlayOnPrepare = true
|
||||
setTrack(wantedTrackId)
|
||||
}
|
||||
|
@ -612,6 +619,7 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr
|
|||
trackStateChanged(false)
|
||||
updateMediaSessionState()
|
||||
stopForeground(false)
|
||||
saveTrackProgress()
|
||||
}
|
||||
|
||||
private fun resumeTrack() {
|
||||
|
@ -763,8 +771,9 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr
|
|||
if (mIsThirdPartyIntent) {
|
||||
trackChanged()
|
||||
}
|
||||
} else if (mSetProgressOnPrepare > 0) {
|
||||
mPlayer?.seekTo(mSetProgressOnPrepare)
|
||||
}
|
||||
if (mSetProgressOnPrepare > 0) {
|
||||
mp.seekTo(mSetProgressOnPrepare)
|
||||
broadcastTrackProgress(mSetProgressOnPrepare / 1000)
|
||||
mSetProgressOnPrepare = 0
|
||||
}
|
||||
|
@ -844,13 +853,7 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr
|
|||
EventBus.getDefault().post(Events.TrackChanged(mCurrTrack))
|
||||
broadcastNextTrackChange()
|
||||
}
|
||||
|
||||
ensureBackgroundThread {
|
||||
queueDAO.resetCurrent()
|
||||
if (mCurrTrack != null && mPlayer != null) {
|
||||
queueDAO.saveCurrentTrack(mCurrTrack!!.mediaStoreId, 0)
|
||||
}
|
||||
}
|
||||
saveTrackProgress()
|
||||
}
|
||||
|
||||
private fun broadcastTrackStateChange(isPlaying: Boolean) {
|
||||
|
@ -1050,6 +1053,7 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr
|
|||
|
||||
private fun updateProgress(progress: Int) {
|
||||
mPlayer!!.seekTo(progress * 1000)
|
||||
saveTrackProgress()
|
||||
resumeTrack()
|
||||
}
|
||||
|
||||
|
@ -1151,4 +1155,17 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun saveTrackProgress() {
|
||||
if (mCurrTrack != null && mPlayer != null) {
|
||||
ensureBackgroundThread {
|
||||
val trackId = mCurrTrack!!.mediaStoreId
|
||||
val position = mPlayer!!.currentPosition
|
||||
queueDAO.apply {
|
||||
resetCurrent()
|
||||
saveCurrentTrack(trackId, position)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue