• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

前后端 java 对接海康威视监控-hls实现h5播放

武飞扬头像
云村的王子
帮助1

海康的获取监控预览流的接口当中支持 rtsp、rtmp、hls等协议。
这篇文章主要是说hls协议的。
贴上海康的开发平台地址,其中有对应的API:海康开发平台

1、java层面代码

这里除了main方法之外,有两个方法,分别是:
1)分页获取监控点资源。 即返回所有的监控点信息。
2)获取监控点预览取流。 即根据监控点的唯一标识查询Url流。前端要根据这个流去展示监控视频。

/**
 * @Author hanmw
 **/
public class Test {

    /**
     * 分页获取监控点资源
     */
    public static String getMonitorPoint() throws Exception {
        ArtemisConfig config = new ArtemisConfig();
        config.setHost("120.48.8.80:447"); // 代理API网关nginx服务器ip端口
        config.setAppKey("22363721");  // 秘钥appkey
        config.setAppSecret("jrrNwKMdccHJVggAfeLR");// 秘钥appSecret
        final String getCamsApi =  "/artemis/api/resource/v1/cameras";
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("pageNo", 1);
        jsonObject.put("pageSize", 500);
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", getCamsApi);
            }
        };
        return ArtemisHttpUtil.doPostStringArtemis(config, path, jsonObject.toJSONString(), null, null, "application/json");
    }

    /**
     * 获取监控点预览取流
     */
    public static String getMonitorPointPreview() throws Exception {
        ArtemisConfig config = new ArtemisConfig();
        config.setHost("120.48.8.80:447"); // 代理API网关nginx服务器ip端口
        config.setAppKey("22363721");  // 秘钥appkey
        config.setAppSecret("jrrNwKMdccHJVggAfeLR");// 秘钥appSecret
        final String getCamsApi = "/artemis/api/video/v2/cameras/previewURLs";
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("cameraIndexCode", "1c9ef71e452241a095d2aaaaf1985af9");
        jsonObject.put("streamType", 1);
        jsonObject.put("protocol", "hls");
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", getCamsApi);
            }
        };
        return ArtemisHttpUtil.doPostStringArtemis(config, path, jsonObject.toJSONString(), null, null, "application/json");
    }


    public static void main(String[] args) throws Exception {
        String result = getMonitorPointPreview();
        System.out.println(result);

        JSONObject jsonObject = JSONObject.parseObject(result, JSONObject.class);
        if ("0".equals(jsonObject.get("code").toString()) && "success".equals(jsonObject.get("msg").toString())) {
            String url = jsonObject.getJSONObject("data").get("url").toString();
            System.out.println(url);
        }

    }

第一个方法分页获取监控点资源的返回值 主要是为了第二个方法分页获取监控点资源服务的。
这里我们用的是hls协议,所以可以把streamType传1,取子码流。
transmode 以获取的监控点信息当中的 transType为准,一般都是1 即默认TCP。

学新通

调试的时候记得在工程下面引入海康的lib包(文章结尾有下载地址)
学新通

2、海康和对接方方面

将你的合作方账号中的domainId字段改成外网的就可以。
这部分主要是为了让调接口返回的hls 流url是外网可以访问到的。
比如 http://120.207.8.20:83/openUrl/x3KDmM0/live.m3u8

学新通
学新通
学新通

注意:
hls 协议支持的码流编码格式是 h264,音频格式是 aac(前端是复合流时),需要注意前 端的码流编码格式,要检查下前端设备上的编码格式和音频格式是否符合,编码格式是不是h264,音频格式是不是aac,如果不需要音频,可以把视频类型由复合流改为视频流。
学新通

参考文章:https://blog.csdn.net/m0_46690280/article/details/120849969

后端lib包和前端html 以供测试。
链接:https://pan.百度.com/s/1aKvV3BlGyAhOSaXJ8xxXsg
提取码:ghwn

注:下载下来的html文件当中只需要修改这部分,替换为你的 hls 流url就可以。
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhggajfk
系列文章
更多 icon
同类精品
更多 icon
继续加载