Browse Source

Fix media session on old Chrome versions

master
morethanwords 3 years ago
parent
commit
7c4a8b4bbe
  1. 42
      src/components/appMediaPlaybackController.ts

42
src/components/appMediaPlaybackController.ts

@ -69,31 +69,47 @@ class AppMediaPlaybackController {
document.body.append(this.container); document.body.append(this.container);
if(navigator.mediaSession) { if(navigator.mediaSession) {
navigator.mediaSession.setActionHandler('play', this.play); const actions: {[action in MediaSessionAction]?: MediaSessionActionHandler} = {
navigator.mediaSession.setActionHandler('pause', this.pause); play: this.play,
navigator.mediaSession.setActionHandler('stop', this.stop); pause: this.pause,
navigator.mediaSession.setActionHandler('seekbackward', (details) => { stop: this.stop,
seekbackward: this.seekBackward,
seekforward: this.seekForward,
seekto: this.seekTo,
previoustrack: this.previous,
nexttrack: this.next
};
for(const action in actions) {
try {
navigator.mediaSession.setActionHandler(action as MediaSessionAction, actions[action as MediaSessionAction]);
} catch(err) {
console.warn('MediaSession action is not supported:', action);
}
}
}
}
public seekBackward = (details: MediaSessionActionDetails) => {
const media = this.playingMedia const media = this.playingMedia
if(media) { if(media) {
media.currentTime = Math.max(0, media.currentTime - (details.seekOffset || SEEK_OFFSET)); media.currentTime = Math.max(0, media.currentTime - (details.seekOffset || SEEK_OFFSET));
} }
}); };
navigator.mediaSession.setActionHandler('seekforward', (details) => {
public seekForward = (details: MediaSessionActionDetails) => {
const media = this.playingMedia const media = this.playingMedia
if(media) { if(media) {
media.currentTime = Math.min(media.duration, media.currentTime + (details.seekOffset || SEEK_OFFSET)); media.currentTime = Math.min(media.duration, media.currentTime + (details.seekOffset || SEEK_OFFSET));
} }
}); };
navigator.mediaSession.setActionHandler('seekto', (details) => {
public seekTo = (details: MediaSessionActionDetails) => {
const media = this.playingMedia const media = this.playingMedia
if(media) { if(media) {
media.currentTime = details.seekTime; media.currentTime = details.seekTime;
} }
}); };
navigator.mediaSession.setActionHandler('previoustrack', this.previous);
navigator.mediaSession.setActionHandler('nexttrack', this.next);
}
}
public addMedia(peerId: number, doc: MyDocument, mid: number, autoload = true): HTMLMediaElement { public addMedia(peerId: number, doc: MyDocument, mid: number, autoload = true): HTMLMediaElement {
const storage = this.media[peerId] ?? (this.media[peerId] = {}); const storage = this.media[peerId] ?? (this.media[peerId] = {});

Loading…
Cancel
Save