岄. 2024-04-11 14:49 采纳率: 91.7%
浏览 3
已结题

(标签-http|关键词-View)


<!doctype html>
<html>

<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="Pragma" content="no-cache" />
    <meta http-equiv="Cache-Control" content="no-cache, must-revalidate" />
    <meta http-equiv="Expires" content="0" />
</head>
<style>
    .plugin {
        width: 742px;
        height: 310px;
    }
</style>

<body>
    <div>
        <div id="divPlugin" class="plugin"></div>
    </div>
</body>
<script src="../jquery-1.7.1.min.js"></script>
<script src="../codebase/webVideoCtrl.js"></script>
<script src="../codebase/encryption/AES.js"></script>
<script src="../codebase/encryption/cryptico.min.js"></script>
<script src="../codebase/encryption/crypto-3.1.2.min.js"></script>
<script>

    $(function () {
        // 检查插件是否已经安装过
        var iRet = WebVideoCtrl.I_CheckPluginInstall();
        if (-1 == iRet) {
            alert("您还未安装过插件,双击开发包目录里的WebComponentsKit.exe安装!");
            return;
        }

        var oPlugin = {
            iWidth: 742,             // plugin width
            iHeight: 310             // plugin height
        };


        var oLiveView = {
            iProtocol: 1,            // protocol 1:http, 2:https
            szIP: "192.168.2.96",    // protocol ip
            szPort: "80",            // protocol port
            szUsername: "admin",     // device username
            szPassword: "jsrh123456", // device password
            iStreamType: 1,          // stream 1:main stream  2:sub-stream  3:third stream  4:transcode stream
            iChannelID: 1,           // channel no 通道号
            bZeroChannel: false      // zero channel
        };
        // setInterval(() => {
        //     if (oLiveView.iChannelID < 5) {
        //         oLiveView.iChannelID++
        //     }
        // }, 400)
        function startLiveView() {

            // 初始化插件参数及插入插件
            WebVideoCtrl.I_InitPlugin(oPlugin.iWidth, oPlugin.iHeight, {
                bWndFull: true,//是否支持单窗口双击全屏,默认支持 true:支持 false:不支持
                iWndowType: 1,
                cbInitPluginComplete: function () {
                    WebVideoCtrl.I_InsertOBJECTPlugin("divPlugin");

                    // 检查插件是否最新
                    if (-1 == WebVideoCtrl.I_CheckPluginVersion()) {
                        alert("检测到新的插件版本,双击开发包目录里的WebComponentsKit.exe升级!");
                        return;
                    }

                    // 登录设备
                    WebVideoCtrl.I_Login(oLiveView.szIP, oLiveView.iProtocol, oLiveView.szPort, oLiveView.szUsername, oLiveView.szPassword, {
                        success: function (xmlDoc) {
                            // 开始预览
                            var szDeviceIdentify = oLiveView.szIP + "_" + oLiveView.szPort;
                            setTimeout(function () {
                                WebVideoCtrl.I_StartRealPlay(szDeviceIdentify, {
                                    iStreamType: oLiveView.iStreamType,
                                    iChannelID: oLiveView.iChannelID,
                                    bZeroChannel: oLiveView.bZeroChannel
                                });
                            }, 1000);
                            console.log('登录成功');
                        },
                        error: function (status, xmlDoc) {
                            console.log('登录失败');
                        }
                    });
                }
            });
            console.log('startLiveView');
        }
        function updateChannelID() {
            oLiveView.iChannelID++;
            if (oLiveView.iChannelID > 5) {
                oLiveView.iChannelID = 1;
            }
        }

        function executeFunction() {
            updateChannelID();
            startLiveView();
        }

        // 初始执行一次
        executeFunction();

        // 每隔十秒执行一次
        setInterval(executeFunction, 10000);

        // 关闭浏览器
        $(window).unload(function () {
            WebVideoCtrl.I_Stop();
        });
    });

</script>

</html>

我是十秒重新执行一遍这个函数且oLiveView.iChannelID从1到5依次循环。每次循环时,都会重新执行整个函数,并将oLiveView.iChannelID的值更新为当前循环的值。
但是刚进去oLiveView.iChannelID为1时,是可以看到摄像头视频的,但是10秒之后我换个通道执行就是黑的了,为什么?我用的是海康无插件开发3.2包

  • 写回答

3条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-04-11 14:49
    关注

    引自免费微信小程序:皆我百晓生

    在分析您的代码后,我发现了可能造成问题的原因。当您每隔10秒切换通道时,startLiveView() 函数会重新初始化插件、登录设备并开始实时播放。这可能会导致视频流的中断,因为每次调用I_InitPlugin()I_InsertOBJECTPlugin() 都会移除旧的插件实例并创建一个新的实例。为了避免这个问题,您可以尝试仅在插件尚未加载的情况下执行这些操作。为了实现这一点,可以在 cbInitPluginComplete 回调函数中设置一个标志,表示插件已加载,然后在 executeFunction 中检查此标志。以下是修改后的代码:

    <!doctype html>
    <html>
    
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta http-equiv="Pragma" content="no-cache" />
        <meta http-equiv="Cache-Control" content="no-cache, must-revalidate" />
        <meta http-equiv="Expires" content="0" />
    </head>
    <style>
        .plugin {
            width: 742px;
            height: 310px;
        }
    </style>
    
    <body>
        <div>
            <div id="divPlugin" class="plugin"></div>
        </div>
        <script src="../jquery-1.7.1.min.js"></script>
        <script src="../codebase/webVideoCtrl.js"></script>
        <script src="../codebase/encryption/AES.js"></script>
        <script src="../codebase/encryption/cryptico.min.js"></script>
        <script src="../codebase/encryption/crypto-3.1.2.min.js"></script>
        <script>
    
            let isPluginLoaded = false;
    
            $(function () {
                // 检查插件是否已经安装过
                var iRet = WebVideoCtrl.I_CheckPluginInstall();
                if (-1 == iRet) {
                    alert("您还未安装过插件,双击开发包目录里的WebComponentsKit.exe安装!");
                    return;
                }
    
                var oPlugin = {
                    iWidth: 742,             // plugin width
                    iHeight: 310             // plugin height
                };
    
                var oLiveView = {
                    iProtocol: 1,            // protocol 1:http, 2:https
                    szIP: "192.168.2.96",    // protocol ip
                    szPort: "80",            // protocol port
                    szUsername: "admin",     // device username
                    szPassword: "jsrh123456", // device password
                    iStreamType: 1,          // stream 1:main stream  2:sub-stream  3:third stream  4:transcode stream
                    iChannelID: 1,           // channel no 通道号
                    bZeroChannel: false      // zero channel
                };
    
                function startLiveView() {
                    if (!isPluginLoaded) {
                        // 初始化插件参数及插入插件
                        WebVideoCtrl.I_InitPlugin(oPlugin.iWidth, oPlugin.iHeight, {
                            bWndFull: true, // 是否支持单窗口双击全屏,默认支持 true:支持 false:不支持
                            iWndowType: 1,
                            cbInitPluginComplete: function () {
                                WebVideoCtrl.I_InsertOBJECTPlugin("divPlugin");
                                isPluginLoaded = true;
    
                                // 检查插件是否最新
                                if (-1 == WebVideoCtrl.I_CheckPluginVersion()) {
                                    alert("检测到新的插件版本,双击开发包目录里的WebComponentsKit.exe升级!");
                                    return;
                                }
    
                                // 登录设备
                                WebVideoCtrl.I_Login(oLiveView.szIP, oLiveView.iProtocol, oLiveView.szPort, oLiveView.szUsername, oLiveView.szPassword, {
                                    success: function (xmlDoc) {
                                        // 开始预览
                                        var szDeviceIdentify = oLiveView.szIP + "_" + oLiveView.szPort;
                                        setTimeout(function () {
                                            WebVideoCtrl.I_StartRealPlay(szDeviceIdentify, {
                                                iStreamType: oLiveView.iStreamType,
                                                iChannelID: oLiveView.iChannelID,
                                                bZeroChannel: oLiveView.bZeroChannel
                                            });
                                        }, 1000);
                                        console.log('登录成功');
                                    },
                                    error: function (status, xmlDoc) {
                                        console.log('登录失败');
                                    }
                                });
                            }
                        });
                    } else {
                        // 如果插件已加载,只更新通道并开始实时播放
                        updateChannelID();
                        WebVideoCtrl.I_StartRealPlay(oLiveView.szIP + "_" + oLiveView.szPort, {
                            iStreamType: oLiveView.iStreamType,
                            iChannelID: oLiveView.iChannelID,
                            bZeroChannel: oLiveView.bZeroChannel
                        });
                    }
                }
    
                function updateChannelID() {
                    oLiveView.iChannelID++;
                    if (oLiveView.iChannelID > 5) {
                        oLiveView.iChannelID = 1;
                    }
                }
    
                function executeFunction() {
                    updateChannelID();
                    startLiveView();
                }
    
                // 初始执行一次
                executeFunction();
    
                // 每隔十秒执行一次
                setInterval(executeFunction, 10000);
    
                // 关闭浏览器
                $(window).unload(function () {
                    WebVideoCtrl.I_Stop();
                });
            });
    
        </script>
    
    </html>
    

    通过这种方式,我们确保只有在插件未加载时才进行初始化和插入操作。其余情况下,我们只需更新通道并调用I_StartRealPlay 来切换通道,从而避免了频繁重新加载插件的问题。这样应该可以解决视频流中断的问题。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 4月12日
  • 已采纳回答 4月12日
  • 创建了问题 4月11日

悬赏问题

  • ¥15 软件供应链安全是跟可靠性有关还是跟安全性有关?
  • ¥15 电脑蓝屏logfilessrtsrttrail问题
  • ¥20 关于wordpress建站遇到的问题!(语言-php)(相关搜索:云服务器)
  • ¥15 【求职】怎么找到一个周围人素质都很高不会欺负他人,并且未来月薪能够达到一万以上(技术岗)的工作?希望可以收到写有具体,可靠,已经实践过了的路径的回答?
  • ¥15 Java+vue部署版本反编译
  • ¥100 对反编译和ai熟悉的开发者。
  • ¥15 带序列特征的多输出预测模型
  • ¥15 Python 如何安装 distutils模块
  • ¥15 关于#网络#的问题:网络是从楼上引一根网线下来,接了2台傻瓜交换机,也更换了ip还是不行
  • ¥15 资源泄露软件闪退怎么解决?