Skip to content

Commit

Permalink
Wire up PlayPause Action handlers for MediaSession
Browse files Browse the repository at this point in the history
Automagically works == pat on the back for good design?
  • Loading branch information
rtshkmr committed Aug 9, 2024
1 parent 26a1ae9 commit 2a0270c
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions assets/js/hooks/audio_player.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,25 @@ AudioPlayer = {
* */
initMediaSession(playback) {
// TODO: register action handlers
this.registerActionHandlers(playback);
this.updateMediaSession(playback);
},
registerActionHandlers(playback) {
const isSupported = "mediaSession" in navigator;
if (!isSupported) {
return;
}

const session = navigator.mediaSession;
const playPauseEvents = ["play", "pause"];
playPauseEvents.forEach((e) =>
session.setActionHandler(e, (e) => this.dispatchPlayPauseToServer(e)),
);
},
dispatchPlayPauseToServer(e) {
console.log("TRACE Action handler invoked. Type: ", e);
this.pushEvent("play_pause");
},
updateMediaSession(playback) {
const isSupported = "mediaSession" in navigator;
if (!isSupported) {
Expand Down

0 comments on commit 2a0270c

Please sign in to comment.