forked from lavender/watch-party
remember volume between sessions
parent
ef50f2c4d9
commit
af35f9a5cb
|
@ -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;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue