海康相机视频流集成在平台,需要使用海康提供的api,由于个别API不是特别清晰,将一个已经应用的组件共享,减少集成的时间。
总体流程:
1.根据相机绑定的账号获取相关相机信息
2.根据相机信息,使用服务器端技术获取acsessToken
3.根据accessToken和根据官方提供的API获取视频流
4.使用海康提供的插件和展示端进行显示
html
<!--以下代码基于VUE开发-->
<template>
<el-dialog
v-model="dialogVisible"
:title="dialogProps.title"
width="900px"
:close-on-click-modal="false"
:destroy-on-close="true"
@closed="onClosed"
>
<div class="video-preview-container">
<!-- 萤石云播放 -->
<div class="video-wrapper">
<div id="ezPlayerContainer" class="ez-player" ref="videoRef"></div>
</div>
</div>
<template #footer>
<el-button @click="dialogVisible = false">关闭</el-button>
</template>
</el-dialog>
</template>
<script setup lang="ts" name="VideoPreview">
import { ref, onMounted, nextTick } from "vue";
import { ElMessage } from "element-plus";
import { useUserStore } from "@/stores/modules/user";
import axios from "axios";
// 海康提供的播放器插件
import { EZUIKitPlayer } from "ezuikit-js";
// accessToken需要服务器端根据设备信息获取
const accessToken = useUserStore().accessToken;
interface DialogProps {
title: string;
cameraCode: string;
platform: string;
}
const dialogVisible = ref(false);
const dialogProps = ref<DialogProps>({
title: "",
cameraCode: "",
platform: ""
});
const videoRef = ref<HTMLVideoElement | null>(null);
let ezPlayer: any = null;
const initEzPlayer = (streamUrl: string) => {
// 4. 创建播放器实例
ezPlayer = new EZUIKitPlayer({
// 必须与容器 div 的 id 一致
id: "ezPlayerContainer",
// 播放地址 (ezopen协议)
url: streamUrl,
// 授权令牌
accessToken: accessToken,
// 模版决定插件的功能(比如截屏、对话、录音等功能)
template: "security"
});
};
// 接收父组件传过来的参数
const acceptParams = async (params: DialogProps) => {
dialogProps.value = params;
dialogVisible.value = true;
await nextTick();
// 通过调用萤石云接口获取播放视频
axios({
method: "post",
url: "https://open.ys7.com/api/lapp/v2/live/address/get",
headers: {
// 重点:设置 Authorization 头
Authorization: `Bearer ${accessToken}`,
"Content-Type": "application/x-www-form-urlencoded"
},
// 需要再次携带accessToken为参数
data: { accessToken: accessToken, deviceSerial: dialogProps.value.cameraCode } // 直接传入 URLSearchParams 对象,axios 会自动序列化
}).then(res => {
if (res.data.code !== "200") {
ElMessage.error(res.data.msg);
} else {
initEzPlayer(res.data.data.url);
}
});
};
// 关闭时停止播放、清空内存
const onClosed = () => {
if (ezPlayer) {
ezPlayer.stop();
ezPlayer = null;
}
if (videoRef.value) {
videoRef.value.pause();
videoRef.value.src = "";
}
dialogProps.value = {
title: "",
cameraCode: "",
platform: ""
};
};
defineExpose({
acceptParams
});
</script>
<style scoped>
.video-preview-container {
width: 100%;
min-height: 450px;
display: flex;
align-items: center;
justify-content: center;
background-color: #000;
border-radius: 4px;
}
.video-wrapper {
width: 100%;
min-height: 450px;
display: flex;
justify-content: center;
#ezPlayerContainer {
width: 100%;
min-height: 450px;
background-color: #000;
}
}
</style>