diff --git a/src/pages/live/index.tsx b/src/pages/live/index.tsx index aac671d..4521939 100644 --- a/src/pages/live/index.tsx +++ b/src/pages/live/index.tsx @@ -11,9 +11,8 @@ import ButtonBatch from "@/components/button-batch.tsx"; import FlvJs from "flv.js"; import {formatDuration} from "@/util/strings.ts"; import {useSetState} from "ahooks"; -import {set} from "lodash"; -const cache: { flvPlayer?: FlvJs.Player,timerPlayNext?:any,timerLoadState?:any } = {} +const cache: { flvPlayer?: FlvJs.Player,timerPlayNext?:any,timerLoadState?:any,prevUrl?:string } = {} export default function LiveIndex() { const videoRef = useRef(null) const [videoData, setVideoData] = useState([]) @@ -76,23 +75,26 @@ export default function LiveIndex() { return; } if (FlvJs.isSupported()) { + if(cache.prevUrl !== video.video_oss_url) { + // 已经有播放实例 则销毁 + if (cache.flvPlayer) { + cache.flvPlayer.pause() + cache.flvPlayer.unload() + } + cache.prevUrl = video.video_oss_url + cache.flvPlayer = FlvJs.createPlayer({ + type: 'flv', + url: video.video_oss_url + }) - // 已经有播放实例 则销毁 - if (cache.flvPlayer) { - cache.flvPlayer.pause() - cache.flvPlayer.unload() + cache.flvPlayer.attachMediaElement(videoRef.current!) + cache.flvPlayer.load() } - cache.flvPlayer = FlvJs.createPlayer({ - type: 'flv', - url: video.video_oss_url - }) - - cache.flvPlayer.attachMediaElement(videoRef.current!) - cache.flvPlayer.load() if(liveState.live_start_time > 0 && playedTime > 0) videoRef.current!.currentTime = playedTime - cache.flvPlayer.play() + cache.flvPlayer!.play() + cache.timerPlayNext = setTimeout(()=>{ const index = activeToNext(),nextVideo = videoData[index] playVideo(nextVideo,{live_start_time:(Date.now() / 1000 >> 0),id:nextVideo.id}) @@ -118,7 +120,6 @@ export default function LiveIndex() { }); } const clearAllTimer = ()=>{ - if(cache.timerPlayNext) clearTimeout(cache.timerPlayNext) if(cache.timerLoadState) clearTimeout(cache.timerLoadState) } @@ -132,9 +133,9 @@ export default function LiveIndex() { }); } + useEffect(initPlayingState,[videoData]) useEffect(()=>{ loadList() - initPlayingState(); return clearAllTimer; }, [])