properly handle restoring the last played track

This commit is contained in:
tibbi 2020-10-18 18:02:47 +02:00
parent 51725f22f0
commit 34c988d6a4
8 changed files with 48 additions and 10 deletions

View file

@ -130,6 +130,7 @@
<intent-filter>
<action android:name="com.simplemobiletools.musicplayer.action.INIT"/>
<action android:name="com.simplemobiletools.musicplayer.action.INIT_PATH"/>
<action android:name="com.simplemobiletools.musicplayer.action.INIT_QUEUE"/>
<action android:name="com.simplemobiletools.musicplayer.action.FINISH"/>
<action android:name="com.simplemobiletools.musicplayer.action.FINISH_IF_NOT_PLAYING"/>
<action android:name="com.simplemobiletools.musicplayer.action.PREVIOUS"/>

View file

@ -105,6 +105,7 @@ class AlbumsActivity : SimpleActivity() {
private fun updateCurrentTrackBar() {
current_track_bar.updateColors()
current_track_bar.updateCurrentTrack(MusicService.mCurrTrack)
current_track_bar.updateTrackState(MusicService.getIsPlaying())
}
@Subscribe(threadMode = ThreadMode.MAIN)

View file

@ -13,9 +13,7 @@ import androidx.core.view.MenuItemCompat
import androidx.viewpager.widget.ViewPager
import com.simplemobiletools.commons.dialogs.RadioGroupDialog
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.LICENSE_EVENT_BUS
import com.simplemobiletools.commons.helpers.LICENSE_PICASSO
import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_STORAGE
import com.simplemobiletools.commons.helpers.*
import com.simplemobiletools.commons.models.FAQItem
import com.simplemobiletools.commons.models.RadioItem
import com.simplemobiletools.commons.models.Release
@ -25,11 +23,9 @@ import com.simplemobiletools.musicplayer.adapters.ViewPagerAdapter
import com.simplemobiletools.musicplayer.dialogs.ChangeSortingDialog
import com.simplemobiletools.musicplayer.dialogs.SleepTimerCustomDialog
import com.simplemobiletools.musicplayer.extensions.config
import com.simplemobiletools.musicplayer.extensions.queueDAO
import com.simplemobiletools.musicplayer.extensions.sendIntent
import com.simplemobiletools.musicplayer.helpers.FINISH_IF_NOT_PLAYING
import com.simplemobiletools.musicplayer.helpers.REFRESH_LIST
import com.simplemobiletools.musicplayer.helpers.START_SLEEP_TIMER
import com.simplemobiletools.musicplayer.helpers.STOP_SLEEP_TIMER
import com.simplemobiletools.musicplayer.helpers.*
import com.simplemobiletools.musicplayer.interfaces.MainActivityInterface
import com.simplemobiletools.musicplayer.models.Events
import com.simplemobiletools.musicplayer.services.MusicService
@ -169,6 +165,14 @@ class MainActivity : SimpleActivity(), MainActivityInterface {
startActivity(this)
}
}
if (MusicService.mCurrTrack == null) {
ensureBackgroundThread {
if (queueDAO.getAll().isNotEmpty()) {
sendIntent(INIT_QUEUE)
}
}
}
}
private fun initFragments() {
@ -219,6 +223,7 @@ class MainActivity : SimpleActivity(), MainActivityInterface {
private fun updateCurrentTrackBar() {
current_track_bar.updateColors()
current_track_bar.updateCurrentTrack(MusicService.mCurrTrack)
current_track_bar.updateTrackState(MusicService.getIsPlaying())
}
private fun showSleepTimer() {

View file

@ -99,6 +99,7 @@ class TracksActivity : SimpleActivity() {
private fun updateCurrentTrackBar() {
current_track_bar.updateColors()
current_track_bar.updateCurrentTrack(MusicService.mCurrTrack)
current_track_bar.updateTrackState(MusicService.getIsPlaying())
}
@Subscribe(threadMode = ThreadMode.MAIN)

View file

@ -23,6 +23,7 @@ val artworkUri = Uri.parse("content://media/external/audio/albumart")
const val INIT = PATH + "INIT"
const val INIT_PATH = PATH + "INIT_PATH"
const val INIT_QUEUE = PATH + "INIT_QUEUE"
const val FINISH = PATH + "FINISH"
const val FINISH_IF_NOT_PLAYING = PATH + "FINISH_IF_NOT_PLAYING"
const val PREVIOUS = PATH + "PREVIOUS"

View file

@ -14,6 +14,12 @@ interface QueueItemsDao {
@Query("SELECT * FROM queue_items")
fun getAll(): List<QueueItem>
@Query("UPDATE queue_items SET is_current = 0")
fun removeIsCurrent()
@Query("UPDATE queue_items SET is_current = 1 WHERE track_id = :trackId AND playlist_id = :playlistId")
fun setIsCurrent(trackId: Long, playlistId: Int)
@Query("DELETE FROM queue_items")
fun deleteAllItems()
}

View file

@ -11,7 +11,7 @@ data class QueueItem(
@ColumnInfo(name = "track_id") var trackId: Long,
@ColumnInfo(name = "playlist_id") var playlistId: Int,
@ColumnInfo(name = "track_order") var trackOrder: Int,
@ColumnInfo(name = "is_playing") var isPlaying: Boolean,
@ColumnInfo(name = "is_current") var isCurrent: Boolean,
@ColumnInfo(name = "last_position") var lastPosition: Int,
@ColumnInfo(name = "was_played") var wasPlayed: Boolean
)

View file

@ -141,6 +141,7 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr
when (action) {
INIT -> handleInit(intent)
INIT_PATH -> handleInitPath(intent)
INIT_QUEUE -> handleInitQueue()
PREVIOUS -> handlePrevious()
PAUSE -> pauseTrack()
PLAYPAUSE -> handlePlayPause()
@ -196,6 +197,7 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr
initService(intent)
val wantedTrackId = intent?.getLongExtra(TRACK_ID, -1L) ?: -1L
mPlayOnPrepare = true
setTrack(wantedTrackId)
}
}
@ -211,6 +213,19 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr
}
}
private fun handleInitQueue() {
ensureBackgroundThread {
mTracks = getQueuedTracks()
val currentQueueItem = queueDAO.getAll().firstOrNull { it.isCurrent }
if (currentQueueItem != null) {
mCurrTrack = mTracks.firstOrNull { it.id == currentQueueItem.trackId && it.playListId == currentQueueItem.playlistId }
mPlayOnPrepare = false
setTrack(mCurrTrack!!.id)
}
}
}
private fun handlePrevious() {
mPlayOnPrepare = true
playPreviousTrack()
@ -257,7 +272,6 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr
mPlayOnPrepare = false
setupNextTrack()
}
}
}
@ -750,12 +764,21 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr
}
private fun destroyPlayer() {
ensureBackgroundThread {
queueDAO.removeIsCurrent()
if (mCurrTrack != null) {
queueDAO.setIsCurrent(mCurrTrack!!.id, mCurrTrack!!.playListId)
}
mCurrTrack = null
}
mPlayer?.stop()
mPlayer?.release()
mPlayer = null
trackStateChanged(false)
mCurrTrack = null
trackChanged()
mEqualizer?.release()