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);
    }
});

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

相关推荐
大怪v1 小时前
AI抢饭?前端佬:我要验牌!
前端·人工智能·程序员
新酱爱学习1 小时前
字节外包一年,我的技术成长之路
前端·程序员·年终总结
小兵张健1 小时前
开源 playwright-pool 会话池来了
前端·javascript·github
IT_陈寒4 小时前
Python开发者必知的5大性能陷阱:90%的人都踩过的坑!
前端·人工智能·后端
codingWhat5 小时前
介绍一个手势识别库——AlloyFinger
前端·javascript·vue.js
Lee川5 小时前
深度拆解:基于面向对象思维的“就地编辑”组件全模块解析
javascript·架构
代码老中医5 小时前
2026年CSS彻底疯了:这6个新特性让我删掉了三分之一JS代码
前端
进击的尘埃5 小时前
Web Worker 与 OffscreenCanvas:把主线程从重活里解放出来
javascript
不会敲代码15 小时前
Zustand:轻量级状态管理,从入门到实践
前端·typescript
踩着两条虫5 小时前
VTJ.PRO 双向代码转换原理揭秘
前端·vue.js·人工智能