diff --git a/src/Video.web.tsx b/src/Video.web.tsx index e597d154..ad8e17fc 100644 --- a/src/Video.web.tsx +++ b/src/Video.web.tsx @@ -4,6 +4,7 @@ import React, { useEffect, useImperativeHandle, useRef, + useState, type RefObject, } from 'react'; //@ts-ignore @@ -40,6 +41,7 @@ const Video = forwardRef( ) => { const nativeRef = useRef(null); const shakaPlayerRef = useRef(null); + const [ currentSource, setCurrentSource ] = useState(null); const isSeeking = useRef(false); const seek = useCallback( @@ -228,16 +230,28 @@ const Video = forwardRef( nativeRef.current.playbackRate = rate; }, [rate]); - useEffect(() => { - if (!nativeRef.current) { - console.log("Not starting shaka yet bc undefined") - return; - } + + const makeNewShaka = useCallback(() => { if (shakaPlayerRef.current) { shakaPlayerRef.current.unload() } shakaPlayerRef.current = new shaka.Player(); + + + if (source?.cropStart) { + shakaPlayerRef.current.configure({playRangeStart: source?.cropStart / 1000}) + } + if (source?.cropEnd) { + shakaPlayerRef.current.configure({playRangeEnd: source?.cropEnd / 1000}) + } + if (source?.cropStart) { + shakaPlayerRef.current.configure({playRangeStart: source?.cropStart / 1000}) + } + if (source?.cropEnd) { + shakaPlayerRef.current.configure({playRangeEnd: source?.cropEnd / 1000}) + } //@ts-ignore + setCurrentSource(source); shakaPlayerRef.current.addEventListener("error", (event) => { //@ts-ignore const shakaError = event.detail; @@ -250,14 +264,28 @@ const Video = forwardRef( }); }); console.log("Initializing and attaching shaka") - shakaPlayerRef.current.attach(nativeRef.current, true); + //@ts-ignore + shakaPlayerRef.current.attach(nativeRef.current); //@ts-ignore shakaPlayerRef.current.load(source?.uri).then( () => console.log(`${source?.uri} finished loading`) ); console.log("Started shaka loading"); - }, [source, nativeRef.current]) + }, [source, setCurrentSource]); + + const nativeRefDefined = nativeRef.current ? true : false; + + useEffect(() => { + if (!nativeRef.current) { + console.log("Not starting shaka yet bc undefined") + return; + } + if (source !== currentSource) { + console.log("Making new shaka, Old source: ", currentSource, "New source", source); + makeNewShaka() + } + }, [source, nativeRefDefined, currentSource]) useMediaSession(source?.metadata, nativeRef, showNotificationControls);