类型:开发

描述:
This commit is contained in:
2026-01-14 00:51:15 +08:00
parent 4bf2d63aa4
commit ed5876c55b
6 changed files with 87 additions and 140 deletions

View File

@@ -6,8 +6,9 @@
ref="videoElement" ref="videoElement"
muted muted
playsinline playsinline
preload="auto"
autoplay
:controls="false" :controls="false"
disablePictureInPicture
></video> ></video>
<!-- <div v-if="loading" class="loading-overlay pointer-events-none"> <!-- <div v-if="loading" class="loading-overlay pointer-events-none">
<div class="loading-text">{{ loadingText }}</div> <div class="loading-text">{{ loadingText }}</div>
@@ -17,7 +18,7 @@
<script> <script>
import Hls from 'hls.js' import Hls from 'hls.js'
import mpegtsjs from 'mpegts.js' import WebRTCWhep from "whepts";
export default { export default {
name: 'HlsPlayer', name: 'HlsPlayer',
props: { props: {
@@ -163,27 +164,15 @@ export default {
if (this.hls) { if (this.hls) {
this.immediateCleanup() this.immediateCleanup()
} }
if(this.url.startsWith('ws')){ if(this.url.startsWith('http://192.168.77.200:8050/')){
const videoElement = document.getElementById('video') const player = new WebRTCWhep({
const player = mpegtsjs.createPlayer({ url: this.url, // WHEP 服务器地址
url: this.url, container: this.video, // 视频播放容器
type: 'flv', onError: (error) => {
isLive: true, console.error('播放错误:', error)
hasAudio: false }
})
player.attachMediaElement(videoElement)
player.load()
player.play()
// 错误处理和重连机制
player.on('error', (err) => {
player.unload()
// 3 秒后尝试重新加载
setTimeout(() => {
player.load()
player.play()
}, 3000)
}) })
webrtcRefs.push(player)
}else{ }else{
this.hls = new Hls({ this.hls = new Hls({
// 内存优化配置 // 内存优化配置

View File

@@ -43,12 +43,13 @@
import Hls from 'hls.js' import Hls from 'hls.js'
import pubSub from 'pubsub-js' import pubSub from 'pubsub-js'
import mpegtsjs from "mpegts.js"; import WebRTCWhep from "whepts";
let modelValue = defineModel() let modelValue = defineModel()
let isCollect = ref(0) let isCollect = ref(0)
let list = ref([]) let list = ref([])
let hlsRefs = [] let hlsRefs = []
let webrtcRefs = []
let total = ref(0) let total = ref(0)
let src = ref('') let src = ref('')
let cameraIndexCode = ref('') let cameraIndexCode = ref('')
@@ -106,6 +107,12 @@
}) })
hlsRefs = [] hlsRefs = []
} }
if (webrtcRefs.length > 0) {
webrtcRefs.map((item) => {
item.close()
})
webrtcRefs = []
}
} }
const pageNumChange = () => { const pageNumChange = () => {
clearHlsRefs() clearHlsRefs()
@@ -118,29 +125,15 @@
cameraIndexCode: cameraIndexCode cameraIndexCode: cameraIndexCode
}).then(res=>{ }).then(res=>{
const url = res.data.url; const url = res.data.url;
if(url.startsWith('ws')){ if(url.startsWith('http://192.168.77.200:8050/')){
const player = mpegtsjs.createPlayer({ const player = new WebRTCWhep({
url: url, url: url, // WHEP 服务器地址
type: 'flv', container: videoElement, // 视频播放容器
isLive: true, onError: (error) => {
hasAudio: false console.error('播放错误:', error)
}
}) })
player.attachMediaElement(videoElement) webrtcRefs.push(player)
player.load()
player.play()
// 错误处理和重连机制
player.on(mpegtsjs.Events.ERROR, (err) => {
player.unload();
player.destroy();
console.error('播放器错误:', err)
// 3 秒后尝试重新加载
setTimeout(() => {
console.error('重新加载【'+cameraIndexCode+'】' )
createPlayer(cameraIndexCode,videoElement);
}, 3000)
})
hlsRefs.push(player)
} }
else{ else{
const player = new Hls({ const player = new Hls({

View File

@@ -73,7 +73,6 @@ let isCollect = ref(0)
let timer = null let timer = null
let isDiy = ref(0) let isDiy = ref(0)
const handleItem = (item) => { const handleItem = (item) => {
console.log(item,'1111111111111111111111111')
src.value = item.hlsUrl src.value = item.hlsUrl
cameraIndexCode.value = item.cameraIndexCode cameraIndexCode.value = item.cameraIndexCode
isCollect.value = item.isCollect isCollect.value = item.isCollect

View File

@@ -93,7 +93,7 @@ import pubSub from 'pubsub-js'
import Hls from 'hls.js' import Hls from 'hls.js'
import emptyIco from '@/assets/images/n-icon.png' import emptyIco from '@/assets/images/n-icon.png'
import { debounce } from 'lodash' import { debounce } from 'lodash'
import mpegtsjs from "mpegts.js"; import WebRTCWhep from 'whepts'
// const Z00M_IN = 'ZOOM_IN' // 焦距变大 // const Z00M_IN = 'ZOOM_IN' // 焦距变大
// const Z00M_OUT = 'ZOOM_OUT' // 焦距变小 // const Z00M_OUT = 'ZOOM_OUT' // 焦距变小
// const UP = 'UP' // 上转 // const UP = 'UP' // 上转
@@ -111,6 +111,7 @@ const STOP = 'stop' // 停止操作
let cond = ref(false) let cond = ref(false)
let ACTION = '0' let ACTION = '0'
let hlsRefs = [] let hlsRefs = []
let webrtcRefs = []
let hlsRef = null let hlsRef = null
let timer = null let timer = null
let videoLog = ref(1) let videoLog = ref(1)
@@ -260,6 +261,12 @@ const clearHlsRefs = () => {
}) })
hlsRefs = [] hlsRefs = []
} }
if (webrtcRefs.length > 0) {
webrtcRefs.map((item) => {
item.close()
})
webrtcRefs = []
}
} }
// 分页 // 分页
const currentChange = (e) => { const currentChange = (e) => {
@@ -293,29 +300,15 @@ const createPlayer = (cameraIndexCode,videoElement) => {
cameraIndexCode: cameraIndexCode cameraIndexCode: cameraIndexCode
}).then(res=>{ }).then(res=>{
const url = res.data.url; const url = res.data.url;
if(url.startsWith('ws')){ if(url.startsWith('http://192.168.77.200:8050/')){
const player = mpegtsjs.createPlayer({ const player = new WebRTCWhep({
url: url, url: url, // WHEP 服务器地址
type: 'flv', container: videoElement, // 视频播放容器
isLive: true, onError: (error) => {
hasAudio: false console.error('播放错误:', error)
}
}) })
player.attachMediaElement(videoElement) webrtcRefs.push(player)
player.load()
player.play()
// 错误处理和重连机制
player.on(mpegtsjs.Events.ERROR, (err) => {
player.unload();
player.destroy();
console.error('播放器错误:', err)
// 3 秒后尝试重新加载
setTimeout(() => {
console.error('重新加载【'+cameraIndexCode+'】' )
createPlayer(cameraIndexCode,videoElement);
}, 3000)
})
hlsRefs.push(player)
} }
else{ else{
const player = new Hls({ const player = new Hls({

View File

@@ -197,7 +197,7 @@ import pubSub from 'pubsub-js'
import Hls from 'hls.js' import Hls from 'hls.js'
import emptyIco from '@/assets/images/n-icon.png' import emptyIco from '@/assets/images/n-icon.png'
import { debounce } from 'lodash' import { debounce } from 'lodash'
import mpegtsjs from "mpegts.js"; import WebRTCWhep from "whepts";
// const Z00M_IN = 'ZOOM_IN' // 焦距变大 // const Z00M_IN = 'ZOOM_IN' // 焦距变大
// const Z00M_OUT = 'ZOOM_OUT' // 焦距变小 // const Z00M_OUT = 'ZOOM_OUT' // 焦距变小
// const UP = 'UP' // 上转 // const UP = 'UP' // 上转
@@ -215,6 +215,7 @@ const STOP = 'stop' // 停止操作
let cond = ref(false) let cond = ref(false)
let ACTION = '0' let ACTION = '0'
let hlsRefs = [] let hlsRefs = []
let webrtcRefs = []
let hlsRef = null let hlsRef = null
let timer = null let timer = null
let videoLog = ref(1) let videoLog = ref(1)
@@ -364,6 +365,12 @@ const clearHlsRefs = () => {
}) })
hlsRefs = [] hlsRefs = []
} }
if (webrtcRefs.length > 0) {
webrtcRefs.map((item) => {
item.close()
})
webrtcRefs = []
}
} }
// 分页 // 分页
const currentChange = (e) => { const currentChange = (e) => {
@@ -397,29 +404,15 @@ const createPlayer = (cameraIndexCode,videoElement) => {
cameraIndexCode: cameraIndexCode cameraIndexCode: cameraIndexCode
}).then(res=>{ }).then(res=>{
const url = res.data.url; const url = res.data.url;
if(url.startsWith('ws')){ if(url.startsWith('http://192.168.77.200:8050/')){
const player = mpegtsjs.createPlayer({ const player = new WebRTCWhep({
url: url, url: url, // WHEP 服务器地址
type: 'flv', container: videoElement, // 视频播放容器
isLive: true, onError: (error) => {
hasAudio: false console.error('播放错误:', error)
}
}) })
player.attachMediaElement(videoElement) webrtcRefs.push(player)
player.load()
player.play()
// 错误处理和重连机制
player.on(mpegtsjs.Events.ERROR, (err) => {
player.unload();
player.destroy();
console.error('播放器错误:', err)
// 3 秒后尝试重新加载
setTimeout(() => {
console.error('重新加载【'+cameraIndexCode+'】' )
createPlayer(cameraIndexCode,videoElement);
}, 3000)
})
hlsRefs.push(player)
} }
else{ else{
const player = new Hls({ const player = new Hls({

View File

@@ -199,7 +199,7 @@
postVideoControlApi, postVideoControlApi,
postVideoCollectApi postVideoCollectApi
} from '@/api/monitor' } from '@/api/monitor'
import mpegtsjs from 'mpegts.js' import WebRTCWhep from 'whepts'
import { debounce } from 'lodash' import { debounce } from 'lodash'
import pubSub from 'pubsub-js' import pubSub from 'pubsub-js'
@@ -243,6 +243,7 @@
businessVideoDisplayPosition: '' businessVideoDisplayPosition: ''
}) })
let hlsRefs = [] let hlsRefs = []
let webrtcRefs = []
let hlsRef = null let hlsRef = null
let timer = null let timer = null
@@ -260,32 +261,15 @@
cameraIndexCode: cameraIndexCode cameraIndexCode: cameraIndexCode
}).then(res=>{ }).then(res=>{
const url = res.data.url; const url = res.data.url;
if(url.startsWith('ws')){ if(url.startsWith('http://192.168.77.200:8050/')){
const player = mpegtsjs.createPlayer({ const player = new WebRTCWhep({
url: url, url: url, // WHEP 服务器地址
type: 'flv', container: videoElement, // 视频播放容器
isLive: true, onError: (error) => {
hasAudio: false console.error('播放错误:', error)
}
}) })
player.attachMediaElement(videoElement) webrtcRefs.push(player)
player.load()
player.play()
// 错误处理和重连机制
player.on(mpegtsjs.Events.ERROR, (err) => {
console.error('播放器错误【'+cameraIndexCode+'】:', err)
player.unload();
player.destroy();
// 3 秒后尝试重新加载
setTimeout(() => {
console.error('重新加载【'+cameraIndexCode+'】' )
createPlayer(cameraIndexCode,videoElement);
}, 3000)
})
player.on(mpegtsjs.Events.LOADING_COMPLETE, (err) => {
console.error('加载完成:', err)
})
hlsRefs.push(player)
} }
else{ else{
const player = new Hls({ const player = new Hls({
@@ -346,6 +330,16 @@
}) })
hlsRefs = [] hlsRefs = []
} }
if(webrtcRefs.length>0){
webrtcRefs.map((item) => {
try{
item.close()
}catch (e) {
}
})
webrtcRefs = [];
}
} }
const currentChange = (e) => { const currentChange = (e) => {
if(navList.value.length==current.value){ if(navList.value.length==current.value){
@@ -402,27 +396,13 @@
videoLog.value = 2 videoLog.value = 2
cameraIndexCode.value = code cameraIndexCode.value = code
setTimeout(() => { setTimeout(() => {
if (url.startsWith('ws')) { if (url.startsWith('http://192.168.77.200:8050/')) {
hlsRef = mpegtsjs.createPlayer({ hlsRef = new WebRTCWhep({
url: url, url: url, // WHEP 服务器地址
type: 'flv', container: videoRef.value, // 视频播放容器
isLive: true, onError: (error) => {
hasAudio: false console.error('播放错误:', error)
}) }
hlsRef.attachMediaElement(videoRef.value)
hlsRef.load()
hlsRef.play()
// 错误处理和重连机制
hlsRef.on('error', (err) => {
console.error('播放器错误:', err)
// 3 秒后尝试重新加载
hlsRef.unload()
hlsRef.destroy()
// 3 秒后尝试重新加载
setTimeout(() => {
console.error('重新加载【' + cameraIndexCode + '】')
handleItemVideo(url, 101, code, item);
}, 3000)
}) })
} else { } else {
hlsRef = new Hls({ hlsRef = new Hls({