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

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

相关推荐
C_心欲无痕2 小时前
前端如何实现 [记住密码] 功能
前端
qq_316837757 小时前
uni.chooseMedia 读取base64 或 二进制
开发语言·前端·javascript
Zoey的笔记本8 小时前
2026告别僵化工作流:支持自定义字段的看板工具选型与部署指南
大数据·前端·数据库
小二·8 小时前
Python Web 开发进阶实战:混沌工程初探 —— 主动注入故障,构建高韧性系统
开发语言·前端·python
gis开发8 小时前
【无标题】
java·前端·javascript
小二·8 小时前
Python Web 开发进阶实战:低代码平台集成 —— 可视化表单构建器 + 工作流引擎实战
前端·python·低代码
慧一居士9 小时前
Vite 中配置环境变量方法及完整示例
前端·vue.js
梦因you而美9 小时前
XPath 元素定位全方位技术文档
javascript·xpath·xpath定位
天意pt9 小时前
Idempotency 幂等性 - 点赞和投票功能
前端·javascript·express
weixin_4277716110 小时前
cursor 智能commit
前端