react+video:限制快进、倍速、画中画

实现代码:

<video ref={videoRef} src={videoUrl} className={style.video} controls onRateChange={rateChange} onPlay={playVideo} onPause={pauseVideo} onTimeUpdate={timeUpdate} disablePictureInPicture playsInline poster={poster} controlsList="nodownload noremoteplayback noplaybackrate">
     您的浏览器不支持 video 标签。
</video>

js

    const [videoUrl, setVideoUrl] = useState('http://10.*.*.*:4010/file/attachments/20230411150035.mp4');
    const videoRef = useRef(null);
    const lastTime = useRef(0);
    const [isPlaying, setIsPlaying] = useState(false);
    useEffect(() => {
        const videoElement = videoRef.current;
        //限制画中画播放
        const handleEnterPictureInPicture = async () => {
            Toast.show('当前视频不支持画中画播放');
            if (document.pictureInPictureElement) {
                try {
                    await document.exitPictureInPicture();
                } catch (error) {
                    console.error('Error exiting Picture-in-Picture', error);
                }
            }
        };
        // 监听事件
        videoElement.addEventListener('enterpictureinpicture', handleEnterPictureInPicture);
        return () => {
            videoElement.removeEventListener('enterpictureinpicture', handleEnterPictureInPicture);
        };
    }, []);
    //限制倍速
    const rateChange = () => {
        if (videoRef.current.playbackRate !== 1.0) {
            Toast.show('当前视频不支持倍速播放');
            videoRef.current.playbackRate = 1.0;
        }
    };
    
    const playVideo = () => {
        if (!isPlaying) {
            videoRef.current.play();
            setIsPlaying(true);
        }
    };
    const pauseVideo = () => {
        if (isPlaying) {
            videoRef.current.pause();
            setIsPlaying(false);
        }
    };
    //点击实现播放/暂停
    const handlePlayPause = () => {
        if (isPlaying) {
            pauseVideo();
        } else {
            playVideo();
        }
    };
    //限制快进
    const timeUpdate = () => {
        const currentTime = videoRef.current.currentTime;
        console.log(currentTime);
        if (currentTime > lastTime.current + 1) {
            Toast.show('当前视频不支持快进');
            videoRef.current.currentTime = lastTime.current;
            return;
        } else {
            lastTime.current = currentTime;
        }
    };

您好,针对您的问题,我可以为您提供以下的解答: 1. 禁止拖动进度条:可以通过在 video 标签加入属性 `controls="false"` 来禁止掉进度条,示例如下: ``` <video src="{{src}}" controls="false"></video> ``` 2. 可全屏观看:可以通过在 video 标签加入属性 `fullscreen="true"` 来启用全屏功能,示例如下: ``` <video src="{{src}}" fullscreen="true"></video> ``` 3. 视频播放结束事件:可以通过在 video 标签绑定 `bindended` 事件来监听视频播放结束,示例如下: ``` <video src="{{src}}" bindended="onEnded"></video> ``` 4. 数据监听:可以使用小程序框架提供的数据监听机制来监听数据变化,示例如下: ``` Page({ data: { src: "", duration: 0 }, onLoad: function () { this.setData({ src: "http://example.com/video.mp4" }) wx.getVideoInfo({ src: this.data.src, success: (res) => { this.setData({ duration: res.duration }) } }) } }) ``` 5. 显示播放时长:可以通过调用 `wx.getVideoInfo` 方法获取视频时长,并将时长渲染到页面上,示例如下: ``` <video src="{{src}}" bindtimeupdate="onTimeUpdate"></video> Page({ data: { src: "", duration: 0, currentTime: 0 }, onLoad: function () { this.setData({ src: "http://example.com/video.mp4" }) wx.getVideoInfo({ src: this.data.src, success: (res) => { this.setData({ duration: res.duration }) } }) }, onTimeUpdate: function (e) { this.setData({ currentTime: e.detail.currentTime }) } }) ``` 6. 固定倍速:可以通过在 video 标签加入属性 `playbackRate="2"` 来设置播放速度为 2 倍速,示例如下: ``` <video src="{{src}}" playbackRate="2"></video> ``` 7. 视频去除黑边:可以通过在 video 标签加入属性 `objectFit="fill"` 来让视频填充整个容器,去除黑边,示例如下: ``` <video src="{{src}}" objectFit="fill"></video> ``` 希望以上解答能够帮助到您,如有疑问请随时提出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值