feat: add chapter navigation buttons to video player #457
This commit is contained in:
1 file changed
+55
-8
@@ -104,6 +104,7 @@ function addSubtitleTrackToReactPlayer(
|
|||||||
track.srclang = reqSubTrack[0].Language ?? "en";
|
track.srclang = reqSubTrack[0].Language ?? "en";
|
||||||
track.label = reqSubTrack[0].DisplayTitle ?? "Subtitle";
|
track.label = reqSubTrack[0].DisplayTitle ?? "Subtitle";
|
||||||
track.default = true;
|
track.default = true;
|
||||||
|
track.id = subtitleTracks.url;
|
||||||
|
|
||||||
track.addEventListener("load", () => {
|
track.addEventListener("load", () => {
|
||||||
const a = track.track;
|
const a = track.track;
|
||||||
@@ -113,7 +114,14 @@ function addSubtitleTrackToReactPlayer(
|
|||||||
console.error(e);
|
console.error(e);
|
||||||
});
|
});
|
||||||
|
|
||||||
videoElem.appendChild(track);
|
const trackAlreadyExists = videoElem.textTracks.getTrackById(
|
||||||
|
subtitleTracks.url,
|
||||||
|
);
|
||||||
|
if (!trackAlreadyExists?.id) {
|
||||||
|
// console.log(trackAlreadyExists.getTrackById(reqSubTrack[0].Index));
|
||||||
|
videoElem.appendChild(track);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
for (const i of videoElem.textTracks) {
|
for (const i of videoElem.textTracks) {
|
||||||
if (i.label === reqSubTrack[0].DisplayTitle) {
|
if (i.label === reqSubTrack[0].DisplayTitle) {
|
||||||
@@ -487,7 +495,7 @@ function VideoPlayer() {
|
|||||||
let trickplayResolution: TrickplayInfo | undefined;
|
let trickplayResolution: TrickplayInfo | undefined;
|
||||||
const trickplayResolutions = mediaSource.id
|
const trickplayResolutions = mediaSource.id
|
||||||
? item?.Trickplay?.[mediaSource.id]
|
? item?.Trickplay?.[mediaSource.id]
|
||||||
: null;
|
: null;
|
||||||
if (trickplayResolutions) {
|
if (trickplayResolutions) {
|
||||||
let bestWidth: number | undefined;
|
let bestWidth: number | undefined;
|
||||||
const maxWidth = window.screen.width * window.devicePixelRatio * 0.2;
|
const maxWidth = window.screen.width * window.devicePixelRatio * 0.2;
|
||||||
@@ -574,7 +582,7 @@ function VideoPlayer() {
|
|||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (currentChapter?.[0]?.Name) {
|
if (currentChapter?.[0]?.Name) {
|
||||||
return (
|
return (
|
||||||
<div className="flex flex-column video-osb-bubble glass">
|
<div className="flex flex-column video-osb-bubble glass">
|
||||||
@@ -612,7 +620,7 @@ function VideoPlayer() {
|
|||||||
handleExitPlayer(); // Exit player if playback has finished and the queue is empty
|
handleExitPlayer(); // Exit player if playback has finished and the queue is empty
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const [subtitleIsChanging, startSubtitleChange] = useTransition();
|
const [subtitleIsChanging, startSubtitleChange] = useTransition();
|
||||||
const handleSubtitleChange: ChangeEventHandler<
|
const handleSubtitleChange: ChangeEventHandler<
|
||||||
HTMLInputElement | HTMLTextAreaElement
|
HTMLInputElement | HTMLTextAreaElement
|
||||||
@@ -637,7 +645,6 @@ function VideoPlayer() {
|
|||||||
if (api && mediaSource.audio.allTracks) {
|
if (api && mediaSource.audio.allTracks) {
|
||||||
changeAudioTrack(toNumber(e.target.value), api, progress);
|
changeAudioTrack(toNumber(e.target.value), api, progress);
|
||||||
setIsReady(false);
|
setIsReady(false);
|
||||||
console.log("Audio change progress", progress);
|
|
||||||
// setSettingsMenu(null);
|
// setSettingsMenu(null);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -645,6 +652,36 @@ function VideoPlayer() {
|
|||||||
// setPlaying(true);
|
// setPlaying(true);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handlePrevChapter = useCallback(() => {
|
||||||
|
const chapters = item?.Chapters?.filter((chapter, index) => {
|
||||||
|
if ((chapter.StartPositionTicks ?? 0) <= progress) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (!chapters?.length) {
|
||||||
|
player.current?.seekTo(0);
|
||||||
|
}
|
||||||
|
if (chapters?.length === 1) {
|
||||||
|
player.current?.seekTo(ticksToSec(chapters[0].StartPositionTicks ?? 0));
|
||||||
|
} else if (chapters?.length > 1) {
|
||||||
|
player.current?.seekTo(
|
||||||
|
ticksToSec(chapters?.[chapters.length - 2].StartPositionTicks ?? 0),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// const chapter = item?.Chapters?.[Number(currentChapterIndex) - 1];
|
||||||
|
// player.current?.seekTo(ticksToSec(currentChapter?.[0]?.StartPositionTicks ?? 0));
|
||||||
|
}, [progress, playsessionId]);
|
||||||
|
const handleNextChapter = useCallback(() => {
|
||||||
|
const next = item?.Chapters?.filter((chapter, index) => {
|
||||||
|
if ((chapter.StartPositionTicks ?? 0) > progress) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
})[0];
|
||||||
|
console.log("Next Chapter", next);
|
||||||
|
player.current?.seekTo(ticksToSec(next?.StartPositionTicks ?? 0));
|
||||||
|
}, [progress, playsessionId]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="video-player">
|
<div className="video-player">
|
||||||
<AnimatePresence>
|
<AnimatePresence>
|
||||||
@@ -887,21 +924,31 @@ function VideoPlayer() {
|
|||||||
<div className="video-player-osd-controls-buttons">
|
<div className="video-player-osd-controls-buttons">
|
||||||
<PlayPreviousButton />
|
<PlayPreviousButton />
|
||||||
<IconButton
|
<IconButton
|
||||||
onClick={() =>
|
onClick={() => {
|
||||||
player.current?.seekTo(
|
player.current?.seekTo(
|
||||||
player.current.getCurrentTime() - 15,
|
player.current.getCurrentTime() - 15,
|
||||||
)
|
);
|
||||||
}
|
}}
|
||||||
>
|
>
|
||||||
<span className="material-symbols-rounded fill">
|
<span className="material-symbols-rounded fill">
|
||||||
fast_rewind
|
fast_rewind
|
||||||
</span>
|
</span>
|
||||||
</IconButton>
|
</IconButton>
|
||||||
|
<IconButton onClick={handlePrevChapter}>
|
||||||
|
<span className="material-symbols-rounded fill">
|
||||||
|
first_page
|
||||||
|
</span>
|
||||||
|
</IconButton>
|
||||||
<IconButton onClick={() => setPlaying((state) => !state)}>
|
<IconButton onClick={() => setPlaying((state) => !state)}>
|
||||||
<span className="material-symbols-rounded fill">
|
<span className="material-symbols-rounded fill">
|
||||||
{playing ? "pause" : "play_arrow"}
|
{playing ? "pause" : "play_arrow"}
|
||||||
</span>
|
</span>
|
||||||
</IconButton>
|
</IconButton>
|
||||||
|
<IconButton onClick={handleNextChapter}>
|
||||||
|
<span className="material-symbols-rounded fill">
|
||||||
|
last_page
|
||||||
|
</span>
|
||||||
|
</IconButton>
|
||||||
<IconButton
|
<IconButton
|
||||||
onClick={() =>
|
onClick={() =>
|
||||||
player.current?.seekTo(
|
player.current?.seekTo(
|
||||||
|
|||||||
Reference in new issue
Block a user