From eabcf06cb264dfc352a7a0d7d31ad0bae9c89924 Mon Sep 17 00:00:00 2001 From: ad044 Date: Thu, 11 Feb 2021 23:25:38 +0400 Subject: [PATCH] all subtitles should work now --- src/components/MediaScene/MediaPlayer.tsx | 34 +++++++++++++++++------ src/store.ts | 15 ++++++++-- 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/src/components/MediaScene/MediaPlayer.tsx b/src/components/MediaScene/MediaPlayer.tsx index 0501d9b..175effb 100644 --- a/src/components/MediaScene/MediaPlayer.tsx +++ b/src/components/MediaScene/MediaPlayer.tsx @@ -26,8 +26,8 @@ const MediaPlayer = () => { const requestRef = useRef(); const videoRef = createRef(); - const audioRef = createRef(); const trackRef = createRef(); + const subtitleRef = createRef(); // end scene specific stuff const endMediaPlayedCount = useStore((state) => state.endMediaPlayedCount); @@ -38,6 +38,25 @@ const MediaPlayer = () => { (state) => state.resetEndMediaPlayedCount ); + useEffect(() => { + const handleCueChange = (e: any) => { + const { track } = e.target; + const { activeCues } = track; + const text = [...activeCues].map( + (cue) => cue.getCueAsHTML().textContent + )[0]; + if (subtitleRef.current && videoRef.current) { + if (!text || videoRef.current.currentTime === 0) + subtitleRef.current.textContent = text; + else subtitleRef.current.textContent = text; + } + }; + + if (trackRef.current) { + trackRef.current.addEventListener("cuechange", handleCueChange); + } + }, [subtitleRef, trackRef, videoRef]); + const updateTime = useCallback(() => { (requestRef.current as any) = requestAnimationFrame(updateTime); if (videoRef.current) { @@ -96,7 +115,6 @@ const MediaPlayer = () => { }, [endMediaPlayedCount]); useEffect(() => { - console.log("here"); if (currentScene === "end") { if (endMediaPlayedCount === 0) { if (videoRef.current) { @@ -142,11 +160,9 @@ const MediaPlayer = () => { } else { import("../../static/movie/" + mediaToPlay + "[0].webm").then( (media) => { - if (videoRef.current && audioRef.current) { + if (videoRef.current) { videoRef.current.src = media.default; - audioRef.current.src = xa0006; videoRef.current.load(); - audioRef.current.load(); } } ); @@ -167,12 +183,12 @@ const MediaPlayer = () => { return ( <> - -