vue video重复视频 设置 srcObject 视频流不占用资源 减少资源浪费

// 直接设置srcObject减少获取视频流:通过 captureStream() 方法从下方视频元素获取视频流。

// 设置 srcObject:将获取到的视频流设置为上方视频的 srcObject 减少资源浪费

javascript 复制代码
// 获取到需要复制到的dom元素   
const firstVideoElement = proxy.$refs.firstVideo;
// 获取需要复制的视频流元素
const activeVideoElement = proxy.$refs[`videoRefs${index}`][0];
// 赋值给需要显示的dom
firstVideoElement.srcObject = activeVideoElement.captureStream();
javascript 复制代码
onMounted(async () => {
    try {
        const { data } = await getCameraInfo(props.id);
        if (!data) return;
        // console.log("视频监控", data);
        streams.value = data;
        if (streams.value && streams.value.length > 0) {
            await nextTick();
            streams.value.forEach((stream, index) => {
                let videoElement = proxy.$refs["videoRefs" + index][0]; // 通过 ref 获取视频元素
                const webRtcServer = new WebRtcStreamer(videoElement, url);
                webRtcServer.connect(stream.rtspVideoUrl);
                webRtcServers.value.push(webRtcServer); // 存储 WebRtcStreamer 实例
            });
            // 监听 loadedmetadata 事件
            proxy.$refs["videoRefs0"][0].addEventListener('loadedmetadata', () => {
                const firstVideoElement = proxy.$refs.firstVideo;
                const activeVideoElement = proxy.$refs[`videoRefs0`][playNum.value];
                firstVideoElement.srcObject = activeVideoElement.captureStream();
            });
            // nextTick(() => {
            //     // 默认加载第一个视频
            //     const firstVideoElement = proxy.$refs.firstVideo;
            //     // const firstWebRtcServer = new WebRtcStreamer(firstVideoElement, url);
            //     // firstWebRtcServer.connect(streams.value[playNum.value].rtspVideoUrl);
            //     // webRtcServers.value.push(firstWebRtcServer);
            //     const activeVideoElement = proxy.$refs[`videoRefs${index}`][0];
            //     firstVideoElement.srcObject = activeVideoElement.captureStream();
            // });
        }
    } catch (error) {
        console.error('Failed to fetch camera info:', error);
    }
});

原视频流加载完成后,再复制流

相关推荐
@大迁世界18 小时前
Firefox 本地模式:localhost 再见了 ?
前端·架构
米米商聊20 小时前
同名附件为什么被 Map 覆盖?重复键与一对多索引
前端·javascript
echome88821 小时前
Python 异常处理:try、else、finally 的正确搭配
开发语言·前端·python·异常处理·编程技巧
时间的情敌21 小时前
OpenLayers 实战|Vue3 + OpenLayers 完整工程案例(加载天地图、要素、弹窗、绘制、WFS)
前端·openlayers
陈随易21 小时前
下载神器!Motrix 停更三年后重写回归
前端·后端·程序员
前端的日常1 天前
一个人+AI做情侣食谱小程序,0 费用的服务器
前端·后端
计算机魔术师1 天前
谷歌搜索广告踩刹车,AI广告成为救命稻草
前端
总要学点什么1 天前
第81篇:数据可视化-PM2.5分布——让圆点的胖瘦替每座城市报出浓度
前端·信息可视化·echarts·cesium·空间分析
IT_陈寒1 天前
Vue的v-for里用了个临时变量,页面直接挂了
前端·人工智能·后端
hunter_0761 天前
一次 iOS 真机签名 409 的完整排查:证书、设备、描述文件为什么总是"对不上"
前端