修改播放器设置
This commit is contained in:
parent
8e064cb01a
commit
cf698f4456
|
|
@ -151,9 +151,13 @@ const stopFlv = () => {
|
||||||
let hlsPlayer: Hls | null = null
|
let hlsPlayer: Hls | null = null
|
||||||
const hlsVideoRef = ref<HTMLVideoElement | null>(null)
|
const hlsVideoRef = ref<HTMLVideoElement | null>(null)
|
||||||
const isHlsPlaying = ref(false)
|
const isHlsPlaying = ref(false)
|
||||||
|
let hlsRetryCount = 0
|
||||||
|
const hlsMaxRetries = 5
|
||||||
|
let hlsRetryTimer: any = null
|
||||||
|
|
||||||
const playHls = () => {
|
const playHls = () => {
|
||||||
testHlsError.value = ''
|
testHlsError.value = ''
|
||||||
|
hlsRetryCount = 0
|
||||||
|
|
||||||
if (!hlsVideoRef.value) {
|
if (!hlsVideoRef.value) {
|
||||||
testHlsError.value = '视频元素未找到'
|
testHlsError.value = '视频元素未找到'
|
||||||
|
|
@ -165,26 +169,75 @@ const playHls = () => {
|
||||||
hlsPlayer = null
|
hlsPlayer = null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (hlsRetryTimer) {
|
||||||
|
clearTimeout(hlsRetryTimer)
|
||||||
|
hlsRetryTimer = null
|
||||||
|
}
|
||||||
|
|
||||||
if (Hls.isSupported()) {
|
if (Hls.isSupported()) {
|
||||||
try {
|
try {
|
||||||
hlsPlayer = new Hls({
|
hlsPlayer = new Hls({
|
||||||
|
debug: false,
|
||||||
|
enableWorker: true,
|
||||||
|
lowLatencyMode: true,
|
||||||
|
backBufferLength: 90,
|
||||||
xhrSetup: (xhr) => {
|
xhrSetup: (xhr) => {
|
||||||
xhr.withCredentials = true
|
xhr.withCredentials = true
|
||||||
}
|
},
|
||||||
|
manifestLoadingTimeOut: 10000,
|
||||||
|
manifestLoadingMaxRetry: 3,
|
||||||
|
manifestLoadingRetryDelay: 1000,
|
||||||
|
levelLoadingTimeOut: 10000,
|
||||||
|
levelLoadingMaxRetry: 4,
|
||||||
|
fragLoadingTimeOut: 20000,
|
||||||
|
fragLoadingMaxRetry: 6
|
||||||
})
|
})
|
||||||
|
|
||||||
hlsPlayer.loadSource(hlsUrl.value)
|
hlsPlayer.loadSource(hlsUrl.value)
|
||||||
hlsPlayer.attachMedia(hlsVideoRef.value)
|
hlsPlayer.attachMedia(hlsVideoRef.value)
|
||||||
|
|
||||||
hlsPlayer.on(Hls.Events.MANIFEST_PARSED, () => {
|
hlsPlayer.on(Hls.Events.MANIFEST_PARSED, () => {
|
||||||
|
console.log('HLS manifest 解析成功')
|
||||||
hlsVideoRef.value?.play()
|
hlsVideoRef.value?.play()
|
||||||
isHlsPlaying.value = true
|
isHlsPlaying.value = true
|
||||||
|
testHlsError.value = ''
|
||||||
})
|
})
|
||||||
|
|
||||||
hlsPlayer.on(Hls.Events.ERROR, (event, data) => {
|
hlsPlayer.on(Hls.Events.ERROR, (event, data) => {
|
||||||
|
console.error('HLS 错误:', data)
|
||||||
|
|
||||||
if (data.fatal) {
|
if (data.fatal) {
|
||||||
testHlsError.value = `播放错误: ${data.type} - ${data.details}`
|
switch (data.type) {
|
||||||
isHlsPlaying.value = false
|
case Hls.ErrorTypes.NETWORK_ERROR:
|
||||||
|
if (data.details === 'manifestLoadError' && hlsRetryCount < hlsMaxRetries) {
|
||||||
|
// M3U8 文件加载失败,可能是还没生成,等待后重试
|
||||||
|
hlsRetryCount++
|
||||||
|
testHlsError.value = `M3U8 文件加载失败,正在重试 (${hlsRetryCount}/${hlsMaxRetries})...`
|
||||||
|
console.log(`HLS 重试 ${hlsRetryCount}/${hlsMaxRetries}`)
|
||||||
|
|
||||||
|
hlsRetryTimer = setTimeout(() => {
|
||||||
|
if (hlsPlayer) {
|
||||||
|
hlsPlayer.destroy()
|
||||||
|
}
|
||||||
|
playHls()
|
||||||
|
}, 2000) // 等待 2 秒后重试
|
||||||
|
} else {
|
||||||
|
testHlsError.value = `网络错误: ${data.details}`
|
||||||
|
isHlsPlaying.value = false
|
||||||
|
}
|
||||||
|
break
|
||||||
|
case Hls.ErrorTypes.MEDIA_ERROR:
|
||||||
|
testHlsError.value = `媒体错误: ${data.details}`
|
||||||
|
// 尝试恢复
|
||||||
|
if (hlsPlayer) {
|
||||||
|
hlsPlayer.recoverMediaError()
|
||||||
|
}
|
||||||
|
break
|
||||||
|
default:
|
||||||
|
testHlsError.value = `播放错误: ${data.type} - ${data.details}`
|
||||||
|
isHlsPlaying.value = false
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
@ -202,6 +255,10 @@ const playHls = () => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const stopHls = () => {
|
const stopHls = () => {
|
||||||
|
if (hlsRetryTimer) {
|
||||||
|
clearTimeout(hlsRetryTimer)
|
||||||
|
hlsRetryTimer = null
|
||||||
|
}
|
||||||
if (hlsPlayer) {
|
if (hlsPlayer) {
|
||||||
hlsPlayer.destroy()
|
hlsPlayer.destroy()
|
||||||
hlsPlayer = null
|
hlsPlayer = null
|
||||||
|
|
@ -210,6 +267,7 @@ const stopHls = () => {
|
||||||
hlsVideoRef.value.pause()
|
hlsVideoRef.value.pause()
|
||||||
hlsVideoRef.value.src = ''
|
hlsVideoRef.value.src = ''
|
||||||
}
|
}
|
||||||
|
hlsRetryCount = 0
|
||||||
isHlsPlaying.value = false
|
isHlsPlaying.value = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue