feat(video): 添加视频播放功能及缓存版本控制

- 引入useState管理播放状态
- 添加视频播放按钮事件处理函数handlePlay
- 视频元素增加多种属性以兼容移动端播放
- 使用版本号参数解决视频缓存问题
- 使用变量管理视频地址及封面图地址
- 新增移动端播放引导提示文字
This commit is contained in:
2026-04-07 20:57:32 +08:00
parent fa917639d2
commit 23a4e58d91

View File

@@ -1,20 +1,47 @@
import { useEffect } from 'react' import { useEffect, useState } from 'react'
import Taro from '@tarojs/taro';
const videoUrl = 'https://oss.wsdns.cn/20260406/2be0376cac054f2ba86dd35a2bc52e11.mp4'
// 添加版本号解决缓存问题
const videoVersion = 'v=1.0'
const posterUrl = `${videoUrl}?x-oss-process=video/snapshot,t_1000,f_jpg,w_396,h_222,m_fast`
const MyPage = () => { const MyPage = () => {
const [isPlaying, setIsPlaying] = useState(false)
useEffect(() => { useEffect(() => {
}, []) }, [])
// 点击播放按钮
const handlePlay = () => {
const video = document.querySelector('video')
if (video) {
video.play()
setIsPlaying(true)
}
}
return ( return (
<div className={'px-3'}> <div className={'px-3'}>
<video <video
id="homeVideo"
controls controls
playsInline
webkit-playsinline="true"
x5-video-player-type="h5"
x5-video-player-fullscreen="false"
preload="metadata"
className='w-full max-w-2xl mx-auto' className='w-full max-w-2xl mx-auto'
poster='https://oss.wsdns.cn/20260406/2be0376cac054f2ba86dd35a2bc52e11.mp4?x-oss-process=video/snapshot,t_1000,f_jpg,w_396,h_222,m_fast' poster={posterUrl}
> >
<source src='https://oss.wsdns.cn/20260406/2be0376cac054f2ba86dd35a2bc52e11.mp4' type='video/mp4' /> <source src={`${videoUrl}?${videoVersion}`} type='video/mp4' />
</video> </video>
{/* 移动端播放引导可选的UI增强 */}
<div className={'text-center text-gray-400 text-xs mt-2'}>
</div>
</div> </div>
) )
} }