From 9be74c3e74d035abfb79fffc7f3f7180d8429a70 Mon Sep 17 00:00:00 2001 From: mister-ben Date: Fri, 2 Jun 2023 10:10:17 +0200 Subject: [PATCH] move to separate file --- src/js/mediasession.js | 112 +++++++++++++++++++++++++++++++++++++++++ src/js/player.js | 101 +------------------------------------ 2 files changed, 114 insertions(+), 99 deletions(-) create mode 100644 src/js/mediasession.js diff --git a/src/js/mediasession.js b/src/js/mediasession.js new file mode 100644 index 000000000..f0cf7781f --- /dev/null +++ b/src/js/mediasession.js @@ -0,0 +1,112 @@ +import window from 'global/window'; +import {getMimetype} from './utils/mimetypes'; + +/** + * + * Sets up media session if supported and configured + * + * @this Player + */ +export const initMediaSession = function() { + if (!this.options_.mediaSession || !('mediaSession' in window.navigator)) { + return; + } + const ms = window.navigator.mediaSession; + const skipTime = this.options_.mediaSession.skipTime || 15; + + const actionHandlers = [ + ['play', () => { + this.play(); + }], + ['pause', () => { + this.pause(); + }], + ['stop', () => { + this.pause(); + this.currentTime(0); + }], + ['seekbackward', (details) => { + if (this.usingPlugin('ads') && this.ads.inAdBreak()) { + return; + } + this.currentTime(Math.max(0, this.currentTime() - (details.skipOffset || skipTime))); + }], + ['seekforward', (details) => { + if (this.usingPlugin('ads') && this.ads.inAdBreak()) { + return; + } + this.currentTime(Math.min(this.duration(), this.currentTime() + (details.skipOffset || skipTime))); + }], + ['seekto', (details) => { + if (this.usingPlugin('ads') && this.ads.inAdBreak()) { + return; + } + this.currentTime(details.seekTime); + }] + ]; + + for (const [action, handler] of actionHandlers) { + try { + ms.setActionHandler(action, handler); + } catch (error) { + this.log.debug(`Couldn't register media session action "${action}".`); + } + } + + const setUpMediaSessionPlaylist = () => { + try { + ms.setActionHandler('previoustrack', () => { + this.playlist.previous(); + }); + ms.setActionHandler('nexttrack', () => { + this.playlist.next(); + }); + } catch (error) { + this.log('Couldn\'t register playlist media session actions.'); + } + }; + + // Only setup playlist handlers if / when playlist plugin is present + if (this.usingPlugin('playlist')) { + setUpMediaSessionPlaylist(); + } else { + this.on('pluginsetup:playlist', setUpMediaSessionPlaylist); + } + + /** + * @fires Player#updatemediasession + */ + const updateMediaSession = () => { + this.log('updatems'); + const currentMedia = this.getMedia(); + const playlistItem = this.usingPlugin('playlist') ? Object.assign({}, this.playlist()[this.playlist.currentItem()]) : {}; + const mediaSessionData = { + artwork: currentMedia.artwork || playlistItem.artwork || this.poster() ? [{ + src: this.poster(), + type: getMimetype(this.poster()) + }] : [], + title: currentMedia.title || playlistItem.name || '', + artist: currentMedia.artist || playlistItem.artist || '', + album: currentMedia.album || playlistItem.album || '' + }; + + // This allows the metadata to be updated before being set, e.g. if loadmedia() is not used. + this.trigger('updatemediasession', mediaSessionData); + + ms.metadata = new window.MediaMetadata(mediaSessionData); + }; + + const updatePositionState = () => { + ms.setPositionState({ + duration: this.duration(), + playbackRate: this.playbackRate(), + position: this.currentTime() + }); + }; + + this.on('playing', updateMediaSession); + + if ('setPositionState' in ms) { + this.on(['playing', 'seeked', 'ratechange'], updatePositionState); + } +}; diff --git a/src/js/player.js b/src/js/player.js index 9f2eb30bd..e8013f0bb 100644 --- a/src/js/player.js +++ b/src/js/player.js @@ -35,6 +35,7 @@ import {getMimetype, findMimetype} from './utils/mimetypes'; import {hooks} from './utils/hooks'; import {isObject} from './utils/obj'; import keycode from 'keycode'; +import { initMediaSession } from './mediasession.js'; // The following imports are used only to ensure that the corresponding modules // are always included in the video.js package. Importing the modules will @@ -583,106 +584,8 @@ class Player extends Component { this.audioOnlyMode(this.options_.audioOnlyMode); }); - if (options.mediaSession && 'mediaSession' in window.navigator) { - const ms = window.navigator.mediaSession; - const skipTime = options.mediaSession.skipTime || 15; + initMediaSession.bind(this)(); - const actionHandlers = [ - ['play', () => { - this.play(); - }], - ['pause', () => { - this.pause(); - }], - ['stop', () => { - this.pause(); - this.currentTime(0); - }], - ['seekbackward', (details) => { - if (this.usingPlugin('ads') && this.ads.inAdBreak()) { - return; - } - this.currentTime(Math.max(0, this.currentTime() - (details.skipOffset || skipTime))); - }], - ['seekforward', (details) => { - if (this.usingPlugin('ads') && this.ads.inAdBreak()) { - return; - } - this.currentTime(Math.min(this.duration(), this.currentTime() + (details.skipOffset || skipTime))); - }], - ['seekto', (details) => { - if (this.usingPlugin('ads') && this.ads.inAdBreak()) { - return; - } - this.currentTime(details.seekTime); - }] - ]; - - for (const [action, handler] of actionHandlers) { - try { - ms.setActionHandler(action, handler); - } catch (error) { - this.log.debug(`Couldn't register media session action "${action}".`); - } - } - - const setUpMediaSessionPlaylist = () => { - try { - ms.setActionHandler('previoustrack', () => { - this.playlist.previous(); - }); - ms.setActionHandler('nexttrack', () => { - this.playlist.next(); - }); - } catch (error) { - this.log('Couldn\'t register playlist media session actions.'); - } - }; - - // Only setup playlist handlers if / when playlist plugin is present - if (this.usingPlugin('playlist')) { - setUpMediaSessionPlaylist(); - } else { - this.on('pluginsetup:playlist', setUpMediaSessionPlaylist); - } - - /** - * @fires Player#updatemediasession - */ - const updateMediaSession = () => { - this.log('updatems'); - const currentMedia = this.getMedia(); - const playlistItem = this.usingPlugin('playlist') ? Object.assign({}, this.playlist()[this.playlist.currentItem()]) : {}; - const mediaSessionData = { - artwork: currentMedia.artwork || playlistItem.artwork || this.poster() ? [{ - src: this.poster(), - type: getMimetype(this.poster()) - }] : [], - title: currentMedia.title || playlistItem.name || '', - artist: currentMedia.artist || playlistItem.artist || '', - album: currentMedia.album || playlistItem.album || '' - }; - - // This allows the metadata to be updated before being set, e.g. if loadmedia() is not used. - this.trigger('updatemediasession', mediaSessionData); - - ms.metadata = new window.MediaMetadata(mediaSessionData); - }; - - const updatePositionState = () => { - ms.setPositionState({ - duration: this.duration(), - playbackRate: this.playbackRate(), - position: this.currentTime() - }); - }; - - this.on('playing', updateMediaSession); - - if ('setPositionState' in ms) { - this.on(['playing', 'seeked', 'ratechange'], updatePositionState); - } - } } /**