From 43d898a7ada632afa8b7f4ef7d82277f67aa18dc Mon Sep 17 00:00:00 2001 From: casey langen Date: Sun, 17 Feb 2019 13:55:19 -0800 Subject: [PATCH] OK, found reliable repro for the lock screen bug... and I think I found a hack to fix it. --- .../streaming/StreamingPlaybackService.kt | 3 +- .../remote/service/system/SystemService.kt | 37 ++++++++++--------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/playback/impl/streaming/StreamingPlaybackService.kt b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/playback/impl/streaming/StreamingPlaybackService.kt index b427161e6..3b14230e9 100644 --- a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/playback/impl/streaming/StreamingPlaybackService.kt +++ b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/playback/impl/streaming/StreamingPlaybackService.kt @@ -225,8 +225,8 @@ class StreamingPlaybackService(context: Context) : IPlaybackService { killAudioFocus() if (playContext.currentPlayer != null) { - playContext.currentPlayer?.pause() state = PlaybackState.Paused + playContext.currentPlayer?.pause() } } } @@ -754,6 +754,7 @@ class StreamingPlaybackService(context: Context) : IPlaybackService { } private fun schedulePausedSleep() { + handler.removeCallbacks(pauseServiceSleepRunnable) handler.postDelayed(pauseServiceSleepRunnable, PAUSED_SERVICE_SLEEP_DELAY_MS.toLong()) } diff --git a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/system/SystemService.kt b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/system/SystemService.kt index d030601e9..f35377e97 100644 --- a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/system/SystemService.kt +++ b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/service/system/SystemService.kt @@ -135,6 +135,7 @@ class SystemService : Service() { } checkInitMediaSession() + updateMediaSessionPlaybackState() } private fun shutdownNow() { @@ -174,8 +175,6 @@ class SystemService : Service() { this.setCallback(mediaSessionCallback) this.isActive = true } - - updateMediaSessionPlaybackState() } } @@ -198,28 +197,30 @@ class SystemService : Service() { } private fun updateMediaSessionPlaybackState() { - var mediaSessionState = PlaybackStateCompat.STATE_STOPPED + mediaSession?.let { session -> + var mediaSessionState = PlaybackStateCompat.STATE_STOPPED + var duration = 0 + var playing: ITrack? = null - var duration = 0 - var playing: ITrack? = null - - playback?.let { - when (it.state) { - PlaybackState.Playing -> mediaSessionState = PlaybackStateCompat.STATE_PLAYING - PlaybackState.Buffering -> mediaSessionState = PlaybackStateCompat.STATE_BUFFERING - PlaybackState.Paused -> mediaSessionState = PlaybackStateCompat.STATE_PAUSED - else -> { } + playback?.let { pb -> + mediaSessionState = when (pb.state) { + PlaybackState.Playing -> PlaybackStateCompat.STATE_PLAYING + PlaybackState.Buffering -> PlaybackStateCompat.STATE_BUFFERING + /* HACK: if we return STATE_PAUSED, the playback controls will disappear + from the lock screen! wtf? we don't want that, so we return + STATE_BUFFERING instead, which seems to keep the play controls visible, + and also display the "PLAY" button like the user expects. sigh */ + PlaybackState.Paused -> PlaybackStateCompat.STATE_BUFFERING + PlaybackState.Stopped -> PlaybackStateCompat.STATE_STOPPED + } + playing = pb.playingTrack + duration = (pb.duration * 1000).toInt() } - playing = it.playingTrack - duration = (it.duration * 1000).toInt() - } - - mediaSession?.let { updateMediaSession(playing, duration) updateNotification(playing, mediaSessionState) - it.setPlaybackState(PlaybackStateCompat.Builder() + session.setPlaybackState(PlaybackStateCompat.Builder() .setState(mediaSessionState, 0, 0f) .setActions(MEDIA_SESSION_ACTIONS) .build())