remember volume between sessions

votekiss
maia arson crimew 2022-02-14 17:34:56 +01:00
parent ef50f2c4d9
commit af35f9a5cb
1 changed files with 38 additions and 10 deletions

View File

@ -1,3 +1,27 @@
const loadVolume = () => {
try {
const savedVolume = localStorage.getItem("watch-party-volume");
if (savedVolume != null && savedVolume != "") {
return +savedVolume;
}
} catch (_err) {
// Sometimes localStorage is blocked from use
}
// default
return 0.5;
};
/**
* @param {number} volume
*/
const saveVolume = (volume) => {
try {
localStorage.setItem("watch-party-volume", volume);
} catch (_err) {
// see loadVolume
}
};
/** /**
* @param {string} videoUrl * @param {string} videoUrl
* @param {{name: string, url: string}[]} subtitles * @param {{name: string, url: string}[]} subtitles
@ -6,9 +30,13 @@ const createVideoElement = (videoUrl, subtitles) => {
const video = document.createElement("video"); const video = document.createElement("video");
video.controls = true; video.controls = true;
video.autoplay = false; video.autoplay = false;
video.volume = 0.5; video.volume = loadVolume();
video.crossOrigin = "anonymous"; video.crossOrigin = "anonymous";
video.addEventListener("volumechange", async () => {
saveVolume(video.volume);
});
const source = document.createElement("source"); const source = document.createElement("source");
source.src = videoUrl; source.src = videoUrl;
@ -47,15 +75,15 @@ const createVideoElement = (videoUrl, subtitles) => {
navigator.mediaSession.setActionHandler("skipad", null); navigator.mediaSession.setActionHandler("skipad", null);
} else { } else {
// disable media button support by ignoring the events // disable media button support by ignoring the events
navigator.mediaSession.setActionHandler("play", () => { }); navigator.mediaSession.setActionHandler("play", () => {});
navigator.mediaSession.setActionHandler("pause", () => { }); navigator.mediaSession.setActionHandler("pause", () => {});
navigator.mediaSession.setActionHandler("stop", () => { }); navigator.mediaSession.setActionHandler("stop", () => {});
navigator.mediaSession.setActionHandler("seekbackward", () => { }); navigator.mediaSession.setActionHandler("seekbackward", () => {});
navigator.mediaSession.setActionHandler("seekforward", () => { }); navigator.mediaSession.setActionHandler("seekforward", () => {});
navigator.mediaSession.setActionHandler("seekto", () => { }); navigator.mediaSession.setActionHandler("seekto", () => {});
navigator.mediaSession.setActionHandler("previoustrack", () => { }); navigator.mediaSession.setActionHandler("previoustrack", () => {});
navigator.mediaSession.setActionHandler("nexttrack", () => { }); navigator.mediaSession.setActionHandler("nexttrack", () => {});
navigator.mediaSession.setActionHandler("skipad", () => { }); navigator.mediaSession.setActionHandler("skipad", () => {});
} }
return; return;
} }