Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Persist user volume preference in localStorage #1685

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/components/VideoPlayer2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export const VideoPlayer: FunctionComponent<VideoPlayerProps> = ({
if (!player) return;

const savedCaptionSetting = localStorage.getItem('captionSetting');
const savedvolumeLevel = localStorage.getItem('volumeLevel');
const tracks = player.textTracks();

if (savedCaptionSetting && player) {
Expand All @@ -114,6 +115,14 @@ export const VideoPlayer: FunctionComponent<VideoPlayerProps> = ({
}
}

if (savedvolumeLevel) {
player.volume(parseFloat(savedvolumeLevel).toFixed(2));
} else {
const defaultVolume = 1;
player.volume(defaultVolume);
localStorage.setItem('volumeLevel', defaultVolume.toString());
}

const handleTrackChange = () => {
for (let i = 0; i < tracks.length; i++) {
const track = tracks[i];
Expand All @@ -125,12 +134,25 @@ export const VideoPlayer: FunctionComponent<VideoPlayerProps> = ({
}
};

const handleVolumeChange = () => {
const updateVolume = () => {
localStorage.setItem('volumeLevel', player.volume().toFixed(2).toString());
};
player.on('volumechange', updateVolume);
return () => {
player.off('volumechange', updateVolume);
};
};

const detachVolumeChange = handleVolumeChange();
handleTrackChange();

return () => {
for (let i = 0; i < tracks.length; i++) {
const track = tracks[i];
track.removeEventListener('modechange', handleTrackChange);
}
detachVolumeChange();
};
}, [player]);

Expand Down Expand Up @@ -207,6 +229,7 @@ export const VideoPlayer: FunctionComponent<VideoPlayerProps> = ({
);
}, 1000);
player.volume(VOLUME_LEVELS[newIndexUp]);
localStorage.setItem('volumeLevel', player.volume().toFixed(2));
event.stopPropagation();
break;
case 'ArrowDown': // Decrease volume
Expand All @@ -220,6 +243,7 @@ export const VideoPlayer: FunctionComponent<VideoPlayerProps> = ({
);
}, 1000);
player.volume(VOLUME_LEVELS[newIndexDown]);
localStorage.setItem('volumeLevel', player.volume().toFixed(2));
event.stopPropagation();
break;
}
Expand Down Expand Up @@ -248,11 +272,13 @@ export const VideoPlayer: FunctionComponent<VideoPlayerProps> = ({
case 'ArrowUp': // Arrow up for increasing volume
event.preventDefault();
player.volume(player.volume() + 0.1);
localStorage.setItem('volumeLevel', player.volume().toFixed(2));
event.stopPropagation();
break;
case 'ArrowDown': // Arow dowwn for decreasing volume
event.preventDefault();
player.volume(player.volume() - 0.1);
localStorage.setItem('volumeLevel', player.volume().toFixed(2));
event.stopPropagation();
break;
case 'KeyF': // F key for fullscreen
Expand Down