Skip to content

Commit

Permalink
feat: persist caption settings in localStorage on track mode change (c…
Browse files Browse the repository at this point in the history
  • Loading branch information
IkramBagban committed Oct 21, 2024
1 parent 5802927 commit b5365c3
Showing 1 changed file with 48 additions and 5 deletions.
53 changes: 48 additions & 5 deletions src/components/VideoPlayer2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,52 @@ export const VideoPlayer: FunctionComponent<VideoPlayerProps> = ({
return pipButtonContainer;
};

// Fetch previously saved captions setting from localStorage
useEffect(() => {
if (!player) return;

const savedCaptionSetting = localStorage.getItem('captionSetting');

if (savedCaptionSetting && player) {
const tracks = player.textTracks();

for (let i = 0; i < tracks.length; i++) {
const track = tracks[i];

if (track) {
track.mode =
savedCaptionSetting === 'showing' ? 'showing' : 'disabled';
}
}
}
}, [player]);

useEffect(() => {
if (!player) return;

const handleTrackChange = () => {
const tracks = player.textTracks();
for (let i = 0; i < tracks.length; i++) {
const track = tracks[i];
if (track.kind === 'subtitles' && track.language === 'en') {
// Save the mode to localStorage when it changes
track.addEventListener('modechange', () => {
localStorage.setItem('captionSetting', track.mode);
});
}
}
};

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

useEffect(() => {
const t = searchParams.get('timestamp');
if (contentId && player && !t) {
Expand Down Expand Up @@ -255,11 +301,8 @@ export const VideoPlayer: FunctionComponent<VideoPlayerProps> = ({
const track = tracks[i];

if (track.kind === 'subtitles' && track.language === 'en') {
if (track.mode === 'hidden') {
track.mode = 'showing';
} else {
track.mode = 'hidden';
}
if (track.mode === 'disabled') track.mode = 'showing';
else track.mode = 'disabled';
}
}
event.stopPropagation();
Expand Down

0 comments on commit b5365c3

Please sign in to comment.