From c30f2fb98e280b321715465f554c13a4448c3487 Mon Sep 17 00:00:00 2001 From: lareinayanyu Date: Thu, 9 Jan 2025 19:42:51 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E8=B0=83=E8=AF=95=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../runtime/components/react/mpx-video.tsx | 57 +++++++++---------- 1 file changed, 28 insertions(+), 29 deletions(-) diff --git a/packages/webpack-plugin/lib/runtime/components/react/mpx-video.tsx b/packages/webpack-plugin/lib/runtime/components/react/mpx-video.tsx index fa9bfbed5..02417fb42 100644 --- a/packages/webpack-plugin/lib/runtime/components/react/mpx-video.tsx +++ b/packages/webpack-plugin/lib/runtime/components/react/mpx-video.tsx @@ -80,10 +80,10 @@ interface VideoProps { autoplay?: boolean; loop?: boolean; muted?: boolean; - initialTime?: number; controls?: boolean; poster?: string; style?: ViewStyle; + 'initial-time'?: number; 'object-fit'?: null | 'contain' | 'fill' | 'cover'; 'is-drm'?: boolean; 'provision-url'?: string; @@ -133,7 +133,6 @@ const MpxVideo = forwardRef, VideoProps>((videoProp autoplay = false, loop = false, muted = false, - initialTime = 0, controls = true, poster = '', bindplay, @@ -148,6 +147,7 @@ const MpxVideo = forwardRef, VideoProps>((videoProp bindcontrolstoggle, bindseekcomplete, style, + 'initial-time': initialTime = 0, 'object-fit': objectFit = 'contain', 'is-drm': isDrm = false, 'provision-url': provisionUrl, @@ -166,10 +166,12 @@ const MpxVideo = forwardRef, VideoProps>((videoProp const viewRef = useRef(null) - const videoInfoRef = useRef(null) + const videoInfoRef = useRef({} as VideoInfoData) const propsRef = useRef({}) + const bufferedPercentage = useRef() + propsRef.current = props const { normalStyle, hasSelfPercent, setWidth, setHeight } = @@ -201,57 +203,54 @@ const MpxVideo = forwardRef, VideoProps>((videoProp } }) - // 处理播放进度更新 - const bufferedPercentage = useRef(0) function handleProgress (data: OnProgressData) { - const { playableDuration, seekableDuration, currentTime } = data + const { playableDuration, currentTime } = data bindtimeupdate && bindtimeupdate( getCustomEvent('timeupdate', {}, { detail: { currentTime, - duration: videoInfoRef.current && videoInfoRef.current.duration + duration: videoInfoRef.current.duration }, layoutRef }, propsRef.current ) ) - - if (seekableDuration > 0) { - // 计算缓冲的百分比 - const currentBufferedPercentage = (playableDuration / seekableDuration) * 100 - if (currentBufferedPercentage !== bufferedPercentage.current) { - bufferedPercentage.current = currentBufferedPercentage - bindprogress && bindprogress( - getCustomEvent('progress', - {}, - { - detail: { - buffered: bufferedPercentage.current - }, - layoutRef + // 计算缓冲的百分比 + const duration = videoInfoRef.current.duration || 0 + let currentBufferedPercentage = (playableDuration / duration) * 100 + // playableDuration 比 duration 更精确,会精确到小数,可能会出现大于 100 的情况 + if (currentBufferedPercentage > 100) { + currentBufferedPercentage = 100 + } + if (currentBufferedPercentage !== bufferedPercentage.current) { + bufferedPercentage.current = currentBufferedPercentage + bindprogress && bindprogress( + getCustomEvent('progress', + {}, + { + detail: { + buffered: currentBufferedPercentage || 0 }, - propsRef.current - )) - } + layoutRef + }, + propsRef.current + )) } } - // 处理播放结束 function handleEnd () { bindended!(getCustomEvent('end', {}, { layoutRef }, propsRef.current)) } - // 处理等待 function handleWaiting ({ isBuffering }: OnBufferData) { if (isBuffering) { bindwaiting!(getCustomEvent('waiting', {}, { layoutRef }, propsRef.current)) } } - // 处理seek完成 function handleSeekcomplete ({ seekTime }: OnSeekData) { bindseekcomplete!( getCustomEvent('seekcomplete', @@ -302,7 +301,7 @@ const MpxVideo = forwardRef, VideoProps>((videoProp function handleVideoLoad (data: VideoInfoData) { const { naturalSize, duration } = data - if (autoplay) { + if (initialTime) { videoRef.current && videoRef.current.seek(initialTime) } videoInfoRef.current = data @@ -322,7 +321,7 @@ const MpxVideo = forwardRef, VideoProps>((videoProp // 处理错误 function handleError ({ error }: OnVideoErrorData) { - binderror && binderror(getCustomEvent('play', {}, { detail: { error }, layoutRef }, propsRef.current)) + binderror && binderror(getCustomEvent('play', {}, { detail: { errMsg: error.localizedFailureReason }, layoutRef }, propsRef.current)) } function play () {