Fixed an issue where the main playback UI would freeze when skipping an erroring track (in airplane mode, for example).

This commit is contained in:
Antoine POPINEAU 2020-06-20 22:10:13 +02:00
parent 1b98850a9c
commit 18e981fba5
No known key found for this signature in database
GPG Key ID: A78AC64694F84063
4 changed files with 35 additions and 34 deletions

View File

@ -97,12 +97,12 @@ play {
dependencies {
implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar", "*.aar"))))
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.61")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.2")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.2")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.71")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.6")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.6")
implementation("androidx.appcompat:appcompat:1.1.0")
implementation("androidx.core:core-ktx:1.4.0-alpha01")
implementation("androidx.core:core-ktx:1.5.0-alpha01")
implementation("androidx.coordinatorlayout:coordinatorlayout:1.1.0")
implementation("androidx.preference:preference:1.1.1")
implementation("androidx.recyclerview:recyclerview:1.1.0")

View File

@ -72,11 +72,19 @@ class MainActivity : AppCompatActivity() {
.beginTransaction()
.replace(R.id.container, BrowseFragment())
.commit()
if (bus == null) {
watchEventBus()
}
CommandBus.send(Command.RefreshService)
}
override fun onResume() {
super.onResume()
CommandBus.send(Command.RefreshService)
startService(Intent(this, PlayerService::class.java))
DownloadService.start(this, PinService::class.java)
@ -115,12 +123,6 @@ class MainActivity : AppCompatActivity() {
landscape_queue?.let {
supportFragmentManager.beginTransaction().replace(R.id.landscape_queue, LandscapeQueueFragment()).commit()
}
if (bus == null) {
watchEventBus()
}
CommandBus.send(Command.RefreshService)
}
override fun onPause() {
@ -489,12 +491,15 @@ class MainActivity : AppCompatActivity() {
private fun incrementListenCount(track: Track?) {
track?.let {
GlobalScope.launch(IO) {
Fuel
.post(mustNormalizeUrl("/api/v1/history/listenings/"))
.authorize()
.header("Content-Type", "application/json")
.body(Gson().toJson(mapOf("track" to track.id)))
.awaitStringResponse()
try {
Fuel
.post(mustNormalizeUrl("/api/v1/history/listenings/"))
.authorize()
.header("Content-Type", "application/json")
.body(Gson().toJson(mapOf("track" to track.id)))
.awaitStringResponse()
} catch (_: Exception) {
}
}
}
}

View File

@ -135,7 +135,7 @@ class PlayerService : Service() {
EventBus.send(Event.QueueChanged)
if (queue.metadata.isNotEmpty()) {
EventBus.send(Event.RefreshTrack(queue.current(), player.playWhenReady))
EventBus.send(Event.RefreshTrack(queue.current()))
EventBus.send(Event.StateChanged(player.playWhenReady))
}
}
@ -148,12 +148,7 @@ class PlayerService : Service() {
state(true)
EventBus.send(
Event.RefreshTrack(
queue.current(),
true
)
)
EventBus.send(Event.RefreshTrack(queue.current()))
}
is Command.AddToQueue -> queue.append(message.tracks)
@ -167,7 +162,7 @@ class PlayerService : Service() {
state(true)
EventBus.send(Event.RefreshTrack(queue.current(), true))
EventBus.send(Event.RefreshTrack(queue.current()))
}
is Command.ToggleState -> toggle()
@ -351,7 +346,6 @@ class PlayerService : Service() {
when (playbackState) {
Player.STATE_READY -> mediaControlsManager.updateNotification(queue.current(), true)
Player.STATE_BUFFERING -> EventBus.send(Event.Buffering(true))
Player.STATE_IDLE -> state(false)
Player.STATE_ENDED -> EventBus.send(Event.PlaybackStopped)
}
@ -359,7 +353,6 @@ class PlayerService : Service() {
}
false -> {
EventBus.send(Event.StateChanged(false))
EventBus.send(Event.Buffering(false))
if (playbackState == Player.STATE_READY) {
@ -387,7 +380,7 @@ class PlayerService : Service() {
Cache.set(this@PlayerService, "current", queue.current.toString().toByteArray())
EventBus.send(Event.RefreshTrack(queue.current(), true))
EventBus.send(Event.RefreshTrack(queue.current()))
}
override fun onPositionDiscontinuity(reason: Int) {
@ -401,10 +394,12 @@ class PlayerService : Service() {
override fun onPlayerError(error: ExoPlaybackException) {
EventBus.send(Event.PlaybackError(getString(R.string.error_playback)))
queue.current()?.let {
queue.remove(it)
player.prepare(queue.datasources)
}
queue.current++
player.prepare(queue.datasources, true, true)
player.seekTo(queue.current, 0)
player.playWhenReady = true
EventBus.send(Event.RefreshTrack(queue.current()))
}
}

View File

@ -3,6 +3,7 @@ package com.github.apognu.otter.utils
import com.github.apognu.otter.Otter
import com.google.android.exoplayer2.offline.Download
import com.google.android.exoplayer2.offline.DownloadCursor
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.channels.Channel
@ -43,7 +44,7 @@ sealed class Event {
class Buffering(val value: Boolean) : Event()
class TrackPlayed(val track: Track?, val play: Boolean) : Event()
class TrackFinished(val track: Track?) : Event()
class RefreshTrack(val track: Track?, val play: Boolean) : Event()
class RefreshTrack(val track: Track?) : Event()
class StateChanged(val playing: Boolean) : Event()
object QueueChanged : Event()
object RadioStarted : Event()
@ -67,8 +68,8 @@ sealed class Response {
object EventBus {
fun send(event: Event) {
GlobalScope.launch {
Otter.get().eventBus.send(event)
GlobalScope.launch(IO) {
Otter.get().eventBus.offer(event)
}
}