From 7900c2d065a1a3e57b6e7b289a8da54fd2f61879 Mon Sep 17 00:00:00 2001 From: Tony Wasserka <918-neobrain@users.noreply.dev.funkwhale.audio> Date: Wed, 24 Mar 2021 15:46:53 +0100 Subject: [PATCH] Properly handle redundant MediaSession play/pause requests MediaSession pause requests may happen even when Funkwhale is already in a paused state. Previously FW would flip between play/pause without consideration for the current state instead of doing nothing when the playback state matches the requested one. Notably, this made Funkwhale resume audio playback when entering sleep mode on my system. --- front/src/components/Queue.vue | 7 +++--- front/src/components/audio/Player.vue | 14 +++++++----- front/src/store/player.js | 15 ++++++++++++- front/tests/unit/specs/store/player.spec.js | 25 +++++++++++++++++---- 4 files changed, 47 insertions(+), 14 deletions(-) diff --git a/front/src/components/Queue.vue b/front/src/components/Queue.vue index 5b85b2880..623d94460 100644 --- a/front/src/components/Queue.vue +++ b/front/src/components/Queue.vue @@ -96,7 +96,7 @@ v-if="!playing" :title="labels.play" :aria-label="labels.play" - @click.prevent.stop="togglePlay" + @click.prevent.stop="resumePlayback" class="control"> @@ -105,7 +105,7 @@ v-else :title="labels.pause" :aria-label="labels.pause" - @click.prevent.stop="togglePlay" + @click.prevent.stop="pausePlayback" class="control"> @@ -308,7 +308,8 @@ export default { unmute: "player/unmute", clean: "queue/clean", toggleMute: "player/toggleMute", - togglePlay: "player/togglePlay", + resumePlayback: "player/resumePlayback", + pausePlayback: "player/pausePlayback", }), reorder: function(event) { this.$store.commit("queue/reorder", { diff --git a/front/src/components/audio/Player.vue b/front/src/components/audio/Player.vue index e8bbb8dd8..27a7657e8 100644 --- a/front/src/components/audio/Player.vue +++ b/front/src/components/audio/Player.vue @@ -74,7 +74,7 @@ v-if="!playing" :title="labels.play" :aria-label="labels.play" - @click.prevent.stop="togglePlay" + @click.prevent.stop="resumePlayback" class="circular button control"> @@ -82,7 +82,7 @@ v-else :title="labels.pause" :aria-label="labels.pause" - @click.prevent.stop="togglePlay" + @click.prevent.stop="pausePlayback" class="circular button control"> @@ -203,7 +203,7 @@ { @@ -109,6 +109,19 @@ export default { }, 3000) } }, + resumePlayback ({commit, state, dispatch}) { + commit('playing', true) + if (state.errored && state.errorCount < state.maxConsecutiveErrors) { + setTimeout(() => { + if (state.playing) { + dispatch('queue/next', null, {root: true}) + } + }, 3000) + } + }, + pausePlayback ({commit}) { + commit('playing', false) + }, toggleMute({commit, state}) { if (state.volume > 0) { commit('tempVolume', state.volume) diff --git a/front/tests/unit/specs/store/player.spec.js b/front/tests/unit/specs/store/player.spec.js index ac995ab1a..b40642842 100644 --- a/front/tests/unit/specs/store/player.spec.js +++ b/front/tests/unit/specs/store/player.spec.js @@ -112,24 +112,41 @@ describe('store/player', () => { ] }) }) - it('toggle play false', () => { + it('toggle playback false', () => { testAction({ - action: store.actions.togglePlay, + action: store.actions.togglePlayback, params: {state: {playing: false}}, expectedMutations: [ { type: 'playing', payload: true } ] }) }) - it('toggle play true', () => { + it('toggle playback true', () => { testAction({ - action: store.actions.togglePlay, + action: store.actions.togglePlayback, params: {state: {playing: true}}, expectedMutations: [ { type: 'playing', payload: false } ] }) }) + it('resume playback', () => { + testAction({ + action: store.actions.resumePlayback, + params: {state: {}}, + expectedMutations: [ + { type: 'playing', payload: true } + ] + }) + }) + it('pause playback', () => { + testAction({ + action: store.actions.pausePlayback, + expectedMutations: [ + { type: 'playing', payload: false } + ] + }) + }) it('trackEnded', () => { testAction({ action: store.actions.trackEnded,