Cache playback position on pause. Should fix #19.

This commit is contained in:
Antoine POPINEAU 2020-05-29 10:32:09 +02:00
parent 9d0ee7f1b8
commit 55ab0ce71c
No known key found for this signature in database
GPG Key ID: A78AC64694F84063
4 changed files with 23 additions and 6 deletions

View File

@ -261,12 +261,10 @@ class MainActivity : AppCompatActivity() {
now_playing_title.text = track.title
now_playing_album.text = track.artist.name
now_playing_toggle.icon = getDrawable(R.drawable.pause)
now_playing_progress.progress = 0
now_playing_details_title.text = track.title
now_playing_details_artist.text = track.artist.name
now_playing_details_toggle.icon = getDrawable(R.drawable.pause)
now_playing_details_progress.progress = 0
Picasso.get()
.maybeLoad(maybeNormalizeUrl(track.album.cover.original))

View File

@ -38,6 +38,8 @@ class SearchActivity : AppCompatActivity() {
search.setOnQueryTextListener(object : androidx.appcompat.widget.SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean {
search.clearFocus()
query?.let {
val query = URLEncoder.encode(it, "UTF-8")

View File

@ -106,7 +106,14 @@ class PlayerService : Service() {
if (queue.current > -1) {
player.prepare(queue.datasources, true, true)
player.seekTo(queue.current, 0)
Cache.get(this, "progress")?.let { progress ->
player.seekTo(queue.current, progress.readLine().toLong())
val (current, duration, percent) = progress(true)
ProgressBus.send(current, duration, percent)
}
}
registerReceiver(headphonesUnpluggedReceiver, IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY))
@ -257,6 +264,16 @@ class PlayerService : Service() {
@SuppressLint("NewApi")
private fun state(state: Boolean) {
if (!state) {
val (progress, _, _) = progress()
Cache.set(
this@PlayerService,
"progress",
progress.toString().toByteArray()
)
}
if (state && player.playbackState == Player.STATE_IDLE) {
player.prepare(queue.datasources)
}
@ -305,8 +322,8 @@ class PlayerService : Service() {
player.previous()
}
private fun progress(): Triple<Int, Int, Int> {
if (!player.playWhenReady) return progressCache
private fun progress(force: Boolean = false): Triple<Int, Int, Int> {
if (!player.playWhenReady && !force) return progressCache
return queue.current()?.bestUpload()?.let { upload ->
val current = player.currentPosition

View File

@ -20,7 +20,7 @@
android:layout_margin="8dp"
android:alpha="0"
android:visibility="gone"
app:cardCornerRadius="8dp"
app:cardCornerRadius="3dp"
app:cardElevation="12dp"
app:layout_dodgeInsetEdges="bottom"
tools:alpha="1"