Add persist volume plugin (#2448)

This commit is contained in:
WithoutPants 2022-03-31 08:33:16 +11:00 committed by GitHub
parent 510bec655b
commit 6c3b493323
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 0 deletions

View file

@ -6,6 +6,7 @@ import "videojs-seek-buttons";
import "videojs-landscape-fullscreen";
import "./live";
import "./PlaylistButtons";
import "./persist-volume";
import "./markers";
import cx from "classnames";
@ -164,6 +165,7 @@ export const ScenePlayer: React.FC<IScenePlayerProps> = ({
(player as any).markers();
(player as any).offset();
(player as any).persistVolume();
player.focus();
playerRef.current = player;

View file

@ -0,0 +1,30 @@
import videojs, { VideoJsPlayer } from "video.js";
import localForage from "localforage";
const persistVolume = function (this: VideoJsPlayer) {
const player = this;
const levelKey = "volume-level";
const mutedKey = "volume-muted";
player.on("volumechange", function () {
localForage.setItem(levelKey, player.volume());
localForage.setItem(mutedKey, player.muted());
});
localForage.getItem(levelKey).then((value) => {
if (value !== null) {
player.volume(value as number);
}
});
localForage.getItem(mutedKey).then((value) => {
if (value !== null) {
player.muted(value as boolean);
}
});
};
// Register the plugin with video.js.
videojs.registerPlugin("persistVolume", persistVolume);
export default persistVolume;