diff --git a/app/cyp.js b/app/cyp.js index 5810b01..c4416ec 100644 --- a/app/cyp.js +++ b/app/cyp.js @@ -587,6 +587,8 @@ class App extends HTMLElement { await this._connect(); this.dispatchEvent(new CustomEvent("load")); + + this._initMediaHandler(); } attributeChangedCallback(name, oldValue, newValue) { @@ -628,6 +630,49 @@ class App extends HTMLElement { } alert(`Failed to connect to MPD after ${attempts} attempts. Please reload the page to try again.`); } + + _initMediaHandler() { + // check support mediaSession + if (!('mediaSession' in navigator)) { + console.log('mediaSession is not supported'); + return; + } + + // DOM (using media session controls are allowed only if there is audio/video tag) + const audio = node("audio", {loop: true}, "", this); + node("source", {src: 'https://raw.githubusercontent.com/anars/blank-audio/master/10-seconds-of-silence.mp3'}, '', audio); + + // Init event session (play audio) on click (because restrictions by web browsers) + let mediaSessionInit = false; + window.addEventListener('click', () => { + if (mediaSessionInit) return; + mediaSessionInit = true; + audio.play(); + }); + + // mediaSession define metadata + navigator.mediaSession.metadata = new MediaMetadata({ + title: 'Control Your Player' + }); + + // mediaSession define action handlers + navigator.mediaSession.setActionHandler('play', () => { + this.mpd.command("play"); + audio.play(); + }); + navigator.mediaSession.setActionHandler('pause', () => { + this.mpd.command("pause 1"); + audio.pause(); + }); + navigator.mediaSession.setActionHandler('previoustrack', () => { + this.mpd.command("previous"); + audio.play(); + }); + navigator.mediaSession.setActionHandler('nexttrack', () => { + this.mpd.command("next"); + audio.play(); + }); + } } customElements.define("cyp-app", App); diff --git a/app/js/cyp.js b/app/js/cyp.js index ff08227..a1070f7 100644 --- a/app/js/cyp.js +++ b/app/js/cyp.js @@ -17,4 +17,4 @@ function updateSize() { } window.addEventListener("resize", updateSize); -updateSize(); \ No newline at end of file +updateSize(); diff --git a/app/js/elements/app.js b/app/js/elements/app.js index 87382d0..aed9997 100644 --- a/app/js/elements/app.js +++ b/app/js/elements/app.js @@ -26,6 +26,8 @@ class App extends HTMLElement { await this._connect(); this.dispatchEvent(new CustomEvent("load")); + + this._initMediaHandler(); } attributeChangedCallback(name, oldValue, newValue) { @@ -67,6 +69,49 @@ class App extends HTMLElement { } alert(`Failed to connect to MPD after ${attempts} attempts. Please reload the page to try again.`); } + + _initMediaHandler() { + // check support mediaSession + if (!('mediaSession' in navigator)) { + console.log('mediaSession is not supported'); + return; + } + + // DOM (using media session controls are allowed only if there is audio/video tag) + const audio = html.node("audio", {loop: true}, "", this); + html.node("source", {src: 'https://raw.githubusercontent.com/anars/blank-audio/master/10-seconds-of-silence.mp3'}, '', audio); + + // Init event session (play audio) on click (because restrictions by web browsers) + let mediaSessionInit = false; + window.addEventListener('click', () => { + if (mediaSessionInit) return; + mediaSessionInit = true; + audio.play(); + }); + + // mediaSession define metadata + navigator.mediaSession.metadata = new MediaMetadata({ + title: 'Control Your Player' + }); + + // mediaSession define action handlers + navigator.mediaSession.setActionHandler('play', () => { + this.mpd.command("play") + audio.play() + }); + navigator.mediaSession.setActionHandler('pause', () => { + this.mpd.command("pause 1") + audio.pause() + }); + navigator.mediaSession.setActionHandler('previoustrack', () => { + this.mpd.command("previous") + audio.play() + }); + navigator.mediaSession.setActionHandler('nexttrack', () => { + this.mpd.command("next") + audio.play() + }); + } } customElements.define("cyp-app", App); @@ -81,4 +126,4 @@ function waitForChildren(app) { const promises = [...unique].map(name => customElements.whenDefined(name)); return Promise.all(promises); -} \ No newline at end of file +}