解决组件丢失

main
king 1 year ago
parent 9119223c80
commit ba21531bba
  1. 46
      src/components/video/video.tsx
  2. 6
      src/pages/business/videoInfo/videoInfo.tsx

@ -7,7 +7,12 @@ import unique_ident from "@/hooks/unique_ident";
const deviation: number = 1 const deviation: number = 1
const HVideo: FC<HVideoOptions> = (opt: HVideoOptions) => { const HVideo: FC<HVideoOptions> = (opt: HVideoOptions) => {
const video = Taro.createVideoContext('myVideo') let video
try {
video = Taro.createVideoContext('myVideo')
} catch (e) {
}
const [currentTime, setCurrentTime] = useState(0) const [currentTime, setCurrentTime] = useState(0)
function onTimeUpdate(event: BaseEventOrig<VideoProps.onTimeUpdateEventDetail>) { function onTimeUpdate(event: BaseEventOrig<VideoProps.onTimeUpdateEventDetail>) {
@ -15,7 +20,7 @@ const HVideo: FC<HVideoOptions> = (opt: HVideoOptions) => {
const time = event.detail.currentTime const time = event.detail.currentTime
/** 前进回退 */ /** 前进回退 */
if (currentTime + deviation < time) { if (currentTime + deviation < time) {
video.seek(currentTime) video?.seek(currentTime)
return return
} }
setCurrentTime(time) setCurrentTime(time)
@ -23,9 +28,9 @@ const HVideo: FC<HVideoOptions> = (opt: HVideoOptions) => {
/** 判断是否进入断点 */ /** 判断是否进入断点 */
opt.breakpoint.forEach(d => { opt.breakpoint.forEach(d => {
if (time < d + deviation && time > d - deviation) { if (time < d + deviation && time > d - deviation) {
video.exitFullScreen() video?.exitFullScreen()
video.pause() video?.pause()
video.seek(d - deviation) video?.seek(d - deviation)
opt.onBreakpoint(d) opt.onBreakpoint(d)
return return
} }
@ -33,8 +38,8 @@ const HVideo: FC<HVideoOptions> = (opt: HVideoOptions) => {
} }
opt.setTime((time: number) => { opt.setTime((time: number) => {
video.seek(time) video?.seek(time)
video.play() video?.play()
}) })
function onEnded() { function onEnded() {
@ -42,28 +47,29 @@ const HVideo: FC<HVideoOptions> = (opt: HVideoOptions) => {
if (currentTime + 1 > opt.duration) { if (currentTime + 1 > opt.duration) {
opt.onEnded() opt.onEnded()
} else { } else {
video.seek(currentTime) video?.seek(currentTime)
video.play() video?.play()
} }
} }
Taro.useUnload(() => { Taro.useUnload(() => {
video?.stop()
unique_ident.put(Number(currentTime.toFixed(2)), Date.now()) unique_ident.put(Number(currentTime.toFixed(2)), Date.now())
}) })
return ( return (
<Video <Video
id={'myVideo'} id={'myVideo'}
autoplay autoplay
style={{width: '100%', height: '100%', position: "relative"}} style={{width: '100%', height: '100%', position: "relative"}}
poster={opt?.poster || ''} poster={opt?.poster || ''}
src={opt.src} src={opt.src}
enableProgressGesture={false} enableProgressGesture={false}
direction={90} direction={90}
onTimeUpdate={onTimeUpdate} onTimeUpdate={onTimeUpdate}
onEnded={onEnded} onEnded={onEnded}
/> />
) )
} }

@ -6,8 +6,6 @@ import Catalogue from "./components/catalogue";
import Course from "./components/course"; import Course from "./components/course";
import Taro from "@tarojs/taro"; import Taro from "@tarojs/taro";
import eventsIndex from "@/hooks/eventsIndex"; import eventsIndex from "@/hooks/eventsIndex";
// import eventsIndex from "@/hooks/eventsIndex";
const VideoInfo: FC = () => { const VideoInfo: FC = () => {
const {id, depId} = Taro.getCurrentInstance()?.router?.params as any const {id, depId} = Taro.getCurrentInstance()?.router?.params as any
@ -21,8 +19,7 @@ const VideoInfo: FC = () => {
if (res) { if (res) {
setData(res) setData(res)
} }
/** 用于自动播放 判断当前课程是否完成 */ if (playId) { // 用于自动播放 判断当前课程是否完成
if (playId) {
currentVideo(res) currentVideo(res)
} }
} }
@ -90,6 +87,7 @@ const VideoInfo: FC = () => {
}) })
return ( return (
<> <>
<View className='content'> <View className='content'>

Loading…
Cancel
Save