fixed: 直播状态更新

This commit is contained in:
LittleBoy 2024-12-16 21:56:57 +08:00
parent 74b52562f4
commit 99d7787b04

View File

@ -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<HTMLVideoElement | null>(null)
const [videoData, setVideoData] = useState<LiveVideoInfo[]>([])
@ -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;
}, [])