当前位置: 首页 > news >正文

uni-app + Vue3 开发H5 页面播放海康ws(Websocket协议)的视频流

一、准备工作

1、海康开放平台H5视频播放器开发包下载

海康开放平台

先注册/登录,然后下载开发包

二、播放视频流

1、引入下载的开发包

在项目中创建文件夹 /static/js/Hk,将下载好的开发包解压放到 Hk 文件夹中,

2、引入

在 index.html 文件中引入

<script src="/static/js/HK/h5player.min.js"></script>

如下图:

3、使用

第一步:// 动态加载 h5player.min.js

// 动态加载 h5player.min.js
function loadh5player() {return new Promise((resolve, reject) => {if (typeof JSPlugin !== "function") {resolve();return;}const script = document.createElement("script");script.src = "/static/js/Hk/h5player.min.js";script.onload = resolve;script.onerror = reject;document.head.appendChild(script);});
}	

第二步:初始化播放窗口

// 初始化播放窗口
playersH5.value = new JSPlugin({szId: "player", //需要英文字母开头 必填szBasePath: "/static/js/Hk/h5player.min.js", // 必填,引用H5player.min.js的js相对路径oStyle: {border: "none",borderSelect: "none",background: "#000",}
})

第二步:播放

function playVideo() {playersH5.value.JS_Play(playerUrl.value, 1);// 事件初始化playersH5.value.JS_SetWindowControlCallback({windowEventSelect: function(iWndIndex) { //插件选中窗口回调console.log('windowSelect callback: ', iWndIndex);},pluginErrorHandler: function(iWndIndex, iErrorCode, oError) { //插件错误回调console.log('pluginError callback: ', iWndIndex, iErrorCode, oError);},windowEventOver: function(iWndIndex) { //鼠标移过回调console.log(iWndIndex);},windowEventOut: function(iWndIndex) { //鼠标移出回调console.log(iWndIndex);},windowEventUp: function(iWndIndex) { //鼠标mouseup事件回调console.log(iWndIndex);},windowFullCcreenChange: function(bFull) { //全屏切换回调console.log('fullScreen callback: ', bFull);},firstFrameDisplay: function(iWndIndex, iWidth, iHeight) { //首帧显示回调console.log('firstFrame loaded callback: ', iWndIndex, iWidth, iHeight);},performanceLack: function(iWndIndex) { //性能不足回调console.log('performanceLack callback: ', iWndIndex);},StreamEnd: function(iWndIndex) { //性能不足回调console.log('recv StreamEnd: ', iWndIndex);},StreamHeadChanged: function(iWndIndex) {console.log('recv StreamHeadChanged: ', iWndIndex);},ThumbnailsEvent: (iWndIndex, eventType, eventCode) => {console.log('recv ThumbnailsEvent: ' + iWndIndex + ", eventType:" + eventType + ", eventCode:" +eventCode);},InterruptStream: (iWndIndex, iTime) => {console.log('recv InterruptStream: ' + iWndIndex + ", iTime:" + iTime);},ElementChanged: (iWndIndex, szElementType) => { //回调采用的是video还是canvas console.log('recv ElementChanged: ' + iWndIndex + ", szElementType:" + szElementType);},});}

三、完整代码

父组件:点击方法跳转到视频播放组件页面

function openVideoPlayBack(item) {uni.navigateTo({url: `/pages/videoPlayback/videoPlayback?url=${item.url}`})
}

子组件videoPlayback.vue:视频播放组件

<template><view class="container"><view class="box" id='player' :style="{'height':heightInfo + 'px'}" :info='playerUrl' :stop='stopPlay'></view><view class="tipName">双击全屏展示</view></view>
</template><script setup>import {ref} from "vue";import {onLoad} from "@dcloudio/uni-app"// 接收页面跳转的传参onLoad((e) => {let {url = null} = e;initPlayers(url)})let playersH5 = ref({}); // 存储播放器实例let heightInfo = ref();//销毁播放窗口状态let stopPlay = ref(false);let playerUrl = ref({})// 初始化播放器async function initPlayers(url) {try {playerUrl.value = url;const systemInfo = uni.getSystemInfoSync(); // 获取系统信息heightInfo.value = (systemInfo.windowHeight)/2;await loadh5player();playersH5.value = new JSPlugin({szId: "player", //需要英文字母开头 必填szBasePath: "/static/js/Hk/h5player.min.js", // 必填,引用H5player.min.js的js相对路径oStyle: {border: "none",borderSelect: "none",background: "#000",}})setTimeout(() => {playVideo()}, 500)} catch (error) {console.error("Failed to initialize JSPlugin:", error);}}function playVideo() {playersH5.value.JS_Play(playerUrl.value, 1);// 事件回调绑定playersH5.value.JS_SetWindowControlCallback({windowEventSelect: function(iWndIndex) { //插件选中窗口回调console.log('windowSelect callback: ', iWndIndex);},pluginErrorHandler: function(iWndIndex, iErrorCode, oError) { //插件错误回调console.log('pluginError callback: ', iWndIndex, iErrorCode, oError);},windowEventOver: function(iWndIndex) { //鼠标移过回调console.log(iWndIndex);},windowEventOut: function(iWndIndex) { //鼠标移出回调console.log(iWndIndex);},windowEventUp: function(iWndIndex) { //鼠标mouseup事件回调console.log(iWndIndex);},windowFullCcreenChange: function(bFull) { //全屏切换回调console.log('fullScreen callback: ', bFull);},firstFrameDisplay: function(iWndIndex, iWidth, iHeight) { //首帧显示回调console.log('firstFrame loaded callback: ', iWndIndex, iWidth, iHeight);},performanceLack: function(iWndIndex) { //性能不足回调console.log('performanceLack callback: ', iWndIndex);},StreamEnd: function(iWndIndex) { //性能不足回调console.log('recv StreamEnd: ', iWndIndex);},StreamHeadChanged: function(iWndIndex) {console.log('recv StreamHeadChanged: ', iWndIndex);},ThumbnailsEvent: (iWndIndex, eventType, eventCode) => {console.log('recv ThumbnailsEvent: ' + iWndIndex + ", eventType:" + eventType + ", eventCode:" +eventCode);},InterruptStream: (iWndIndex, iTime) => {console.log('recv InterruptStream: ' + iWndIndex + ", iTime:" + iTime);},ElementChanged: (iWndIndex, szElementType) => { //回调采用的是video还是canvas console.log('recv ElementChanged: ' + iWndIndex + ", szElementType:" + szElementType);},});}// 动态加载 h5player.min.jsfunction loadh5player() {return new Promise((resolve, reject) => {if (typeof JSPlugin !== "function") {resolve();return;}const script = document.createElement("script");script.src = "/static/js/Hk/h5player.min.js";script.onload = resolve;script.onerror = reject;document.head.appendChild(script);});}
</script><style lang="scss" scoped>.container {width: 100%;height: 600px;display: flex;justify-content: center;align-items: center;flex-direction: column;+// padding-top: 44px;.video-container {width: 100%;}.tipName {margin-top: 10px;color: #3709ee;}}
</style>

http://www.xdnf.cn/news/1385767.html

相关文章:

  • 腾讯位置商业授权微信小程序距离计算
  • 有鹿机器人:用智能清洁重塑多行业工作方式
  • AI推介-大语言模型LLMs论文速览(arXiv方向):2025.04.25-2025.04.30
  • ADO 操作access
  • 选华为实验工具:eNSP Pro 和社区在线实验哪个更适合?
  • 《华为战略管理法:DSTE 实战体系》读书笔记
  • 第二章 Vue + Three.js 实现鼠标拖拽旋转 3D 立方体交互实践
  • FDTD_mie散射_项目研究(1)
  • DirectX修复工具官方中文增强版下载!下载安装教程(附安装包),0xc000007b错误解决办法
  • 【python+requests】接口自动化测试:三步用代理工具快速定位问题
  • Linux 软件编程(十四)网络编程:数据存储与 SQLite 数据库
  • 【C++】类与对象(上)
  • Python- Visual Studio Code配置Anaconda
  • Vue 实战:优雅实现无限层级评论区,支持“显示全部”分页递归加载
  • simd笔记
  • 使用生成对抗网络增强网络入侵检测性能
  • 【开题答辩全过程】以 基于Python的美食点评系统为例,包含答辩的问题和答案
  • 【数据结构与算法-Day 20】从零到一掌握二叉树:定义、性质、特殊形态与存储结构全解析
  • Hadoop(六)
  • T06_循环神经网络
  • 基于博客系统的自动化测试项目
  • Selenium无法定位元素的几种解决方案
  • C# 日志写入loki
  • 力扣452:用最少数量的箭射爆气球(排序+贪心)
  • 如何编译和使用 tomcat-connectors-1.2.32 源码(连接 Apache 和 Tomcat)​附安装包下载
  • 数据质检之springboot通过yarn调用spark作业实现数据质量检测
  • Dify 1.8.0 全网首发,预告发布
  • 2024-06-13-debian12安装Mariadb-Galera-Cluster+Nginx+Keepalived高可用多主集群
  • 动态UI的秘诀:React中的条件渲染
  • 在PostgreSQL中使用分区技术