当前位置: 首页 > 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.dtcms.com/a/356422.html

相关文章:

  • 学习:uniapp全栈微信小程序vue3后台(6)
  • Uniapp + UView + FastAdmin 性格测试小程序方案
  • 2025最新uni-app横屏适配方案:微信小程序全平台兼容实战
  • 项目一系列-第9章 集成AI千帆大模型
  • 实现自己的AI视频监控系统-第二章-AI分析模块5(重点)
  • js AbortController 实现中断接口请求
  • 【MFC教程】C++基础:01 小黑框跑起来
  • 【MFC应用创建后核心文件详解】项目名.cpp、项目名.h、项目名Dlg.cpp 和 项目名Dlg.h 的区别与作用
  • Java项目打包成EXE全攻略
  • Kafka 副本同步异常与 ISR 收缩故障排查实录
  • C语言————操作符详解
  • 《华为战略管理法:DSTE 实战体系》读书笔记
  • 【完整源码+数据集+部署教程】骨折检测系统源码和数据集:改进yolo11-EfficientHead
  • 【微信小程序】微信小程序基于双token的API请求封装与无感刷新实现方案
  • 华为无线AC主备配置案例
  • KNN算法详解:鸢尾花识别和手写数字识别
  • mysql安全运维之常见攻击类型与防御指南-从SQL注入到权限提升
  • .Net应用程序和SqlServer数据库使用tls加密会话过程
  • DMZ层Nginx TLS 终止与安全接入配置实战20250829
  • C5仅支持20MHZ带宽,如果路由器5Gwifi处于40MHZ带宽信道时,会出现配网失败
  • Git 合并冲突
  • 【网络】snat/MASQUERADE作用和应用场景
  • 【混合开发】Android+WebView视频图片播放硬件加速详解
  • 网页提示UI操作-适应提示,警告,信息——仙盟创梦IDE
  • 嵌入式学习 day61 DHT11、I2C
  • 项目一系列-第8章 性能优化Redis基础
  • Python OpenCV图像处理与深度学习
  • 30分钟入门实战速成Cursor IDE(2)
  • 30分钟入门实战速成Cursor IDE(1)
  • 微硕WINSOK高性能NP沟道MOS管WSP4067在Type-C双向快充电源管理系统中的应用