Browse Source

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

housekeeping/remove-warnings
Antoine POPINEAU 4 years ago
parent
commit
18e981fba5
No known key found for this signature in database
GPG Key ID: A78AC64694F84063
  1. 8
      app/build.gradle.kts
  2. 29
      app/src/main/java/com/github/apognu/otter/activities/MainActivity.kt
  3. 25
      app/src/main/java/com/github/apognu/otter/playback/PlayerService.kt
  4. 7
      app/src/main/java/com/github/apognu/otter/utils/EventBus.kt

8
app/build.gradle.kts

@ -97,12 +97,12 @@ play { @@ -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")

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

@ -72,11 +72,19 @@ class MainActivity : AppCompatActivity() { @@ -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() { @@ -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() { @@ -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) {
}
}
}
}

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

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

7
app/src/main/java/com/github/apognu/otter/utils/EventBus.kt

@ -3,6 +3,7 @@ package com.github.apognu.otter.utils @@ -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 { @@ -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 { @@ -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)
}
}

Loading…
Cancel
Save