From 8061ee29d5315211dcbfbdfbefecdaec460a4965 Mon Sep 17 00:00:00 2001 From: advplyr Date: Sun, 15 May 2022 15:48:41 -0500 Subject: [PATCH] Add:Media session controls and metadata --- client/components/app/StreamContainer.vue | 67 ++++++++++++++++++++++- client/players/PlayerHandler.js | 3 + 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/client/components/app/StreamContainer.vue b/client/components/app/StreamContainer.vue index 2c1516a5..ea140a78 100644 --- a/client/components/app/StreamContainer.vue +++ b/client/components/app/StreamContainer.vue @@ -148,6 +148,7 @@ export default { setPlaying(isPlaying) { this.isPlaying = isPlaying this.$store.commit('setIsPlaying', isPlaying) + this.updateMediaSessionPlaybackState() }, setSleepTimer(seconds) { this.sleepTimerSet = true @@ -240,6 +241,71 @@ export default { this.playerHandler.closePlayer() this.$store.commit('setMediaPlaying', null) }, + mediaSessionPlay() { + console.log('Media session play') + this.playerHandler.play() + }, + mediaSessionPause() { + console.log('Media session pause') + this.playerHandler.pause() + }, + mediaSessionStop() { + console.log('Media session stop') + this.playerHandler.pause() + }, + mediaSessionSeekBackward() { + console.log('Media session seek backward') + this.playerHandler.jumpBackward() + }, + mediaSessionSeekForward() { + console.log('Media session seek forward') + this.playerHandler.jumpForward() + }, + mediaSessionSeekTo(e) { + console.log('Media session seek to', e) + if (e.seekTime !== null && !isNaN(e.seekTime)) { + this.playerHandler.seek(e.seekTime) + } + }, + updateMediaSessionPlaybackState() { + if ('mediaSession' in navigator) { + navigator.mediaSession.playbackState = this.isPlaying ? 'playing' : 'paused' + } + }, + setMediaSession() { + if (!this.streamLibraryItem) { + console.error('setMediaSession: No library item set') + return + } + + if ('mediaSession' in navigator) { + var coverImageSrc = this.$store.getters['globals/getLibraryItemCoverSrc'](this.streamLibraryItem, '/Logo.png') + const artwork = [ + { + src: coverImageSrc + } + ] + + navigator.mediaSession.metadata = new MediaMetadata({ + title: this.title, + artist: this.playerHandler.displayAuthor || this.mediaMetadata.authorName || 'Unknown', + album: this.mediaMetadata.seriesName || '', + artwork + }) + console.log('Set media session metadata', navigator.mediaSession.metadata) + + navigator.mediaSession.setActionHandler('play', this.mediaSessionPlay) + navigator.mediaSession.setActionHandler('pause', this.mediaSessionPause) + navigator.mediaSession.setActionHandler('stop', this.mediaSessionStop) + navigator.mediaSession.setActionHandler('seekbackward', this.mediaSessionSeekBackward) + navigator.mediaSession.setActionHandler('seekforward', this.mediaSessionSeekForward) + navigator.mediaSession.setActionHandler('seekto', this.mediaSessionSeekTo) + // navigator.mediaSession.setActionHandler('previoustrack') + // navigator.mediaSession.setActionHandler('nexttrack') + } else { + console.warn('Media session not available') + } + }, streamProgress(data) { if (!data.numSegments) return var chunks = data.chunks @@ -312,7 +378,6 @@ export default { libraryItem, episodeId }) - this.playerHandler.load(libraryItem, episodeId, true, this.initialPlaybackRate) }, pauseItem() { diff --git a/client/players/PlayerHandler.js b/client/players/PlayerHandler.js index f3414faa..fdc25278 100644 --- a/client/players/PlayerHandler.js +++ b/client/players/PlayerHandler.js @@ -179,6 +179,9 @@ export default class PlayerHandler { } this.player.set(this.libraryItem, audioTracks, this.isHlsTranscode, this.startTime, this.playWhenReady) + + // browser media session api + this.ctx.setMediaSession() } closePlayer() {