Browse Source

Cache playback position on pause. Should fix #19.

housekeeping/remove-warnings
Antoine POPINEAU 5 years ago
parent
commit
55ab0ce71c
No known key found for this signature in database
GPG Key ID: A78AC64694F84063
  1. 2
      app/src/main/java/com/github/apognu/otter/activities/MainActivity.kt
  2. 2
      app/src/main/java/com/github/apognu/otter/activities/SearchActivity.kt
  3. 23
      app/src/main/java/com/github/apognu/otter/playback/PlayerService.kt
  4. 2
      app/src/main/res/layout/activity_main.xml

2
app/src/main/java/com/github/apognu/otter/activities/MainActivity.kt

@ -261,12 +261,10 @@ class MainActivity : AppCompatActivity() { @@ -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))

2
app/src/main/java/com/github/apognu/otter/activities/SearchActivity.kt

@ -38,6 +38,8 @@ class SearchActivity : AppCompatActivity() { @@ -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")

23
app/src/main/java/com/github/apognu/otter/playback/PlayerService.kt

@ -106,7 +106,14 @@ class PlayerService : Service() { @@ -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() { @@ -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() { @@ -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

2
app/src/main/res/layout/activity_main.xml

@ -20,7 +20,7 @@ @@ -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"

Loading…
Cancel
Save