mirror of
https://github.com/kalarina-bit/Fogwave.git
synced 2026-11-22 08:06:09 +00:00
Reconnect the media controller after it disconnects
This commit is contained in:
@@ -48,6 +48,17 @@ class RadioPlayerManager(private val context: Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Without this a controller whose session went away stays non-null and every command is
|
||||||
|
// silently dropped, leaving the UI stuck on "connecting".
|
||||||
|
private val controllerListener = object : MediaController.Listener {
|
||||||
|
override fun onDisconnected(controller: MediaController) {
|
||||||
|
if (released || controller !== this@RadioPlayerManager.controller) return
|
||||||
|
Log.w(TAG, "MediaController disconnected")
|
||||||
|
dropController()
|
||||||
|
_playerState.value = PlayerState.Idle
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Declared after every property it touches so that nothing is read before initialization.
|
// Declared after every property it touches so that nothing is read before initialization.
|
||||||
init {
|
init {
|
||||||
connectToPlaybackService()
|
connectToPlaybackService()
|
||||||
@@ -56,7 +67,9 @@ class RadioPlayerManager(private val context: Context) {
|
|||||||
private fun connectToPlaybackService() {
|
private fun connectToPlaybackService() {
|
||||||
if (released || controllerFuture != null) return
|
if (released || controllerFuture != null) return
|
||||||
val sessionToken = SessionToken(context, ComponentName(context, FogwavePlaybackService::class.java))
|
val sessionToken = SessionToken(context, ComponentName(context, FogwavePlaybackService::class.java))
|
||||||
val future = MediaController.Builder(context, sessionToken).buildAsync()
|
val future = MediaController.Builder(context, sessionToken)
|
||||||
|
.setListener(controllerListener)
|
||||||
|
.buildAsync()
|
||||||
controllerFuture = future
|
controllerFuture = future
|
||||||
future.addListener({
|
future.addListener({
|
||||||
if (released) return@addListener
|
if (released) return@addListener
|
||||||
@@ -83,6 +96,21 @@ class RadioPlayerManager(private val context: Context) {
|
|||||||
}, MoreExecutors.directExecutor())
|
}, MoreExecutors.directExecutor())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** The controller if it is still connected; a stale one is dropped so the next call reconnects. */
|
||||||
|
private fun connectedController(): MediaController? {
|
||||||
|
val c = controller ?: return null
|
||||||
|
if (c.isConnected) return c
|
||||||
|
dropController()
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun dropController() {
|
||||||
|
controller?.removeListener(playerListener)
|
||||||
|
controller = null
|
||||||
|
controllerFuture?.let { MediaController.releaseFuture(it) }
|
||||||
|
controllerFuture = null
|
||||||
|
}
|
||||||
|
|
||||||
private fun deriveState(p: Player): PlayerState {
|
private fun deriveState(p: Player): PlayerState {
|
||||||
p.playerError?.let { return PlayerState.Error(errorMessage(it)) }
|
p.playerError?.let { return PlayerState.Error(errorMessage(it)) }
|
||||||
return when (p.playbackState) {
|
return when (p.playbackState) {
|
||||||
@@ -108,7 +136,7 @@ class RadioPlayerManager(private val context: Context) {
|
|||||||
fun playStation(station: RadioStation) {
|
fun playStation(station: RadioStation) {
|
||||||
Log.d(TAG, "Station requested: ${station.id}")
|
Log.d(TAG, "Station requested: ${station.id}")
|
||||||
currentStation = station
|
currentStation = station
|
||||||
val c = controller
|
val c = connectedController()
|
||||||
if (c == null) {
|
if (c == null) {
|
||||||
pendingStation = station
|
pendingStation = station
|
||||||
_playerState.value = PlayerState.Connecting
|
_playerState.value = PlayerState.Connecting
|
||||||
@@ -127,7 +155,7 @@ class RadioPlayerManager(private val context: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun play() {
|
fun play() {
|
||||||
val c = controller
|
val c = connectedController()
|
||||||
when {
|
when {
|
||||||
c == null || c.mediaItemCount == 0 -> currentStation?.let { playStation(it) }
|
c == null || c.mediaItemCount == 0 -> currentStation?.let { playStation(it) }
|
||||||
// A failed, stopped or finished live stream has to be reopened rather than resumed.
|
// A failed, stopped or finished live stream has to be reopened rather than resumed.
|
||||||
|
|||||||
Reference in New Issue
Block a user