|
|
@ -2,7 +2,10 @@ import { useRef, useMemo, useEffect, useState, useReducer, useContext, useCallba |
|
|
|
import useMandatoryUpdate from '@/utils/useMandatoryUpdate'; |
|
|
|
import useMandatoryUpdate from '@/utils/useMandatoryUpdate'; |
|
|
|
import { FlowContext } from '@/core/context'; |
|
|
|
import { FlowContext } from '@/core/context'; |
|
|
|
|
|
|
|
|
|
|
|
export const useVideo = ({ onPause, onPlay, onTimeChange, onEndEd }: any) => { |
|
|
|
export const useVideo = ( |
|
|
|
|
|
|
|
{ videoElement, onPause, onPlay, onTimeChange, onEndEd }: any, |
|
|
|
|
|
|
|
dep?: any, |
|
|
|
|
|
|
|
) => { |
|
|
|
const forceUpdate = useMandatoryUpdate(); |
|
|
|
const forceUpdate = useMandatoryUpdate(); |
|
|
|
|
|
|
|
|
|
|
|
const reviceProps = useContext(FlowContext); |
|
|
|
const reviceProps = useContext(FlowContext); |
|
|
@ -15,53 +18,44 @@ export const useVideo = ({ onPause, onPlay, onTimeChange, onEndEd }: any) => { |
|
|
|
duration: 0, |
|
|
|
duration: 0, |
|
|
|
bufferedTime: 0, |
|
|
|
bufferedTime: 0, |
|
|
|
isPictureinpicture: false, |
|
|
|
isPictureinpicture: false, |
|
|
|
|
|
|
|
volume: 0, |
|
|
|
|
|
|
|
multiple: 1.0, |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
const videoRef = useRef<HTMLVideoElement | null>(null); |
|
|
|
const videoRef = useRef<HTMLVideoElement>(null!); |
|
|
|
|
|
|
|
|
|
|
|
const interval = useRef<NodeJS.Timeout | null>(null!); |
|
|
|
const interval = useRef<NodeJS.Timeout | null>(null!); |
|
|
|
|
|
|
|
|
|
|
|
videoRef.current = videoEle; |
|
|
|
videoRef.current = videoElement; |
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
useEffect(() => { |
|
|
|
|
|
|
|
forceUpdate(); |
|
|
|
if (videoRef.current) { |
|
|
|
if (videoRef.current) { |
|
|
|
/** |
|
|
|
/** |
|
|
|
* @description 监听总时长 |
|
|
|
* @description 监听总时长 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
videoRef.current.addEventListener('canplay', () => { |
|
|
|
videoRef.current.addEventListener('canplay', () => { |
|
|
|
videoParameter.current = { |
|
|
|
torture({ duration: videoRef.current.duration }); |
|
|
|
...videoParameter.current, |
|
|
|
|
|
|
|
duration: videoRef.current!.duration, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
/** |
|
|
|
/** |
|
|
|
* @description 监听缓冲 |
|
|
|
* @description 监听缓冲 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
videoRef.current.addEventListener('progress', () => { |
|
|
|
videoRef.current.addEventListener('progress', () => { |
|
|
|
if (videoRef.current!.buffered.length >= 1) { |
|
|
|
if (videoRef.current.buffered.length >= 1) { |
|
|
|
videoParameter.current = { |
|
|
|
torture({ bufferedTime: videoRef.current.buffered.end(0) }); |
|
|
|
...videoParameter.current, |
|
|
|
|
|
|
|
bufferedTime: videoRef.current!.buffered.end(0), |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
/** |
|
|
|
/** |
|
|
|
* @description 已经进入画中画 |
|
|
|
* @description 已经进入画中画 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
videoRef.current.addEventListener('enterpictureinpicture', () => { |
|
|
|
videoRef.current.addEventListener('enterpictureinpicture', () => { |
|
|
|
videoParameter.current = { |
|
|
|
torture({ isPictureinpicture: true }); |
|
|
|
...videoParameter.current, |
|
|
|
|
|
|
|
isPictureinpicture: true, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
/** |
|
|
|
/** |
|
|
|
* @description 已退出画中画模式 |
|
|
|
* @description 已退出画中画模式 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
videoRef.current.addEventListener('leavepictureinpicture', () => { |
|
|
|
videoRef.current.addEventListener('leavepictureinpicture', () => { |
|
|
|
videoParameter.current = { |
|
|
|
torture({ isPictureinpicture: false }); |
|
|
|
...videoParameter.current, |
|
|
|
|
|
|
|
isPictureinpicture: false, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
// 定时器用于更新当前视频时间
|
|
|
|
// 定时器用于更新当前视频时间
|
|
|
|
interval.current = setInterval(() => { |
|
|
|
interval.current = setInterval(() => { |
|
|
@ -69,11 +63,13 @@ export const useVideo = ({ onPause, onPlay, onTimeChange, onEndEd }: any) => { |
|
|
|
* 强制更新 |
|
|
|
* 强制更新 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
forceUpdate(); |
|
|
|
forceUpdate(); |
|
|
|
videoParameter.current = { |
|
|
|
|
|
|
|
...videoParameter.current, |
|
|
|
torture({ |
|
|
|
currentTime: videoRef.current!.currentTime, |
|
|
|
currentTime: videoRef.current.currentTime, |
|
|
|
isPlay: videoRef.current!.paused ? false : true, |
|
|
|
isPlay: videoRef.current.paused ? false : true, |
|
|
|
}; |
|
|
|
volume: videoRef.current.volume, |
|
|
|
|
|
|
|
multiple: videoRef.current.playbackRate, |
|
|
|
|
|
|
|
}); |
|
|
|
}, 1); |
|
|
|
}, 1); |
|
|
|
videoRef.current.addEventListener('pause', pauseChange); |
|
|
|
videoRef.current.addEventListener('pause', pauseChange); |
|
|
|
videoRef.current.addEventListener('play', playChange); |
|
|
|
videoRef.current.addEventListener('play', playChange); |
|
|
@ -83,15 +79,21 @@ export const useVideo = ({ onPause, onPlay, onTimeChange, onEndEd }: any) => { |
|
|
|
return () => { |
|
|
|
return () => { |
|
|
|
interval.current && clearInterval(interval.current); |
|
|
|
interval.current && clearInterval(interval.current); |
|
|
|
}; |
|
|
|
}; |
|
|
|
}, [videoEle, videoRef.current]); |
|
|
|
}, [videoRef.current, dep, videoElement]); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const torture = (val: any) => { |
|
|
|
|
|
|
|
videoParameter.current = { ...videoParameter.current, ...val }; |
|
|
|
|
|
|
|
}; |
|
|
|
const pauseChange = () => { |
|
|
|
const pauseChange = () => { |
|
|
|
|
|
|
|
torture({ isPlay: videoRef.current.paused ? false : true }); |
|
|
|
onPause && onPause(videoParameter.current); |
|
|
|
onPause && onPause(videoParameter.current); |
|
|
|
}; |
|
|
|
}; |
|
|
|
const playChange = () => { |
|
|
|
const playChange = () => { |
|
|
|
|
|
|
|
torture({ isPlay: videoRef.current.paused ? false : true }); |
|
|
|
onPlay && onPlay(videoParameter.current); |
|
|
|
onPlay && onPlay(videoParameter.current); |
|
|
|
}; |
|
|
|
}; |
|
|
|
const timeupdate = () => { |
|
|
|
const timeupdate = () => { |
|
|
|
|
|
|
|
torture({ isPlay: videoRef.current.paused ? false : true }); |
|
|
|
onTimeChange && onTimeChange(videoParameter.current); |
|
|
|
onTimeChange && onTimeChange(videoParameter.current); |
|
|
|
}; |
|
|
|
}; |
|
|
|
const endedChange = () => { |
|
|
|
const endedChange = () => { |
|
|
@ -99,17 +101,16 @@ export const useVideo = ({ onPause, onPlay, onTimeChange, onEndEd }: any) => { |
|
|
|
}; |
|
|
|
}; |
|
|
|
const handleChangePlayState = () => { |
|
|
|
const handleChangePlayState = () => { |
|
|
|
if (videoParameter.current.isPlay) { |
|
|
|
if (videoParameter.current.isPlay) { |
|
|
|
videoRef.current!.pause(); |
|
|
|
videoRef.current.pause(); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
videoRef.current!.play(); |
|
|
|
videoRef.current.play(); |
|
|
|
} |
|
|
|
} |
|
|
|
videoParameter.current = { |
|
|
|
|
|
|
|
...videoParameter.current, |
|
|
|
|
|
|
|
isPlay: videoRef.current!.paused ? false : true, |
|
|
|
|
|
|
|
}; |
|
|
|
}; |
|
|
|
}; |
|
|
|
return useMemo( |
|
|
|
return { |
|
|
|
() => ({ |
|
|
|
handleChangePlayState, |
|
|
|
handleChangePlayState, |
|
|
|
...videoParameter.current, |
|
|
|
...videoParameter.current, |
|
|
|
}; |
|
|
|
}), |
|
|
|
|
|
|
|
[videoParameter.current], |
|
|
|
|
|
|
|
); |
|
|
|
}; |
|
|
|
}; |
|
|
|