Skip to content

Commit

Permalink
feat(Demo): Use MediaSession action handler in the demo (#5927)
Browse files Browse the repository at this point in the history
Closes #2812
  • Loading branch information
avelad committed Nov 27, 2023
1 parent 515a4ab commit 078ab36
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
40 changes: 40 additions & 0 deletions demo/main.js
Expand Up @@ -1304,6 +1304,46 @@ shakaDemo.Main = class {
};
metadata.artist = asset.source;
navigator.mediaSession.metadata = new MediaMetadata(metadata);

const addMediaSessionHandler = (type, callback) => {
try {
navigator.mediaSession.setActionHandler(type, (details) => {
callback(details);
});
} catch (error) {
console.warn(
`The "${type}" media session action is not supported.`);
}
};
const commonHandler = (details) => {
switch (details.action) {
case 'pause':
this.video_.pause();
break;
case 'play':
this.video_.play();
break;
case 'seekbackward':
this.video_.currentTime -= (details.seekOffset || 10);
break;
case 'seekforward':
this.video_.currentTime += (details.seekOffset || 10);
break;
case 'stop':
this.unload();
break;
case 'enterpictureinpicture':
this.controls_.togglePiP();
break;
}
};

addMediaSessionHandler('pause', commonHandler);
addMediaSessionHandler('play', commonHandler);
addMediaSessionHandler('seekbackward', commonHandler);
addMediaSessionHandler('seekforward', commonHandler);
addMediaSessionHandler('stop', commonHandler);
addMediaSessionHandler('enterpictureinpicture', commonHandler);
}

if (this.visualizer_ && this.visualizer_.active) {
Expand Down
7 changes: 7 additions & 0 deletions externs/mediasession.js
Expand Up @@ -32,6 +32,13 @@ const MediaSession = class {
/** @type {?MediaMetadata} */
this.metadata;
}

setActionHandler(type, callback) {
/** @type {string} */
this.type = type;
/** @type {!function(!{action: string, seekOffset: ?number})} */
this.callback = callback;
}
};


Expand Down

0 comments on commit 078ab36

Please sign in to comment.