修改播放器设置
This commit is contained in:
parent
8e064cb01a
commit
cf698f4456
|
|
@ -151,9 +151,13 @@ const stopFlv = () => {
|
|||
let hlsPlayer: Hls | null = null
|
||||
const hlsVideoRef = ref<HTMLVideoElement | null>(null)
|
||||
const isHlsPlaying = ref(false)
|
||||
let hlsRetryCount = 0
|
||||
const hlsMaxRetries = 5
|
||||
let hlsRetryTimer: any = null
|
||||
|
||||
const playHls = () => {
|
||||
testHlsError.value = ''
|
||||
hlsRetryCount = 0
|
||||
|
||||
if (!hlsVideoRef.value) {
|
||||
testHlsError.value = '视频元素未找到'
|
||||
|
|
@ -165,26 +169,75 @@ const playHls = () => {
|
|||
hlsPlayer = null
|
||||
}
|
||||
|
||||
if (hlsRetryTimer) {
|
||||
clearTimeout(hlsRetryTimer)
|
||||
hlsRetryTimer = null
|
||||
}
|
||||
|
||||
if (Hls.isSupported()) {
|
||||
try {
|
||||
hlsPlayer = new Hls({
|
||||
debug: false,
|
||||
enableWorker: true,
|
||||
lowLatencyMode: true,
|
||||
backBufferLength: 90,
|
||||
xhrSetup: (xhr) => {
|
||||
xhr.withCredentials = true
|
||||
}
|
||||
},
|
||||
manifestLoadingTimeOut: 10000,
|
||||
manifestLoadingMaxRetry: 3,
|
||||
manifestLoadingRetryDelay: 1000,
|
||||
levelLoadingTimeOut: 10000,
|
||||
levelLoadingMaxRetry: 4,
|
||||
fragLoadingTimeOut: 20000,
|
||||
fragLoadingMaxRetry: 6
|
||||
})
|
||||
|
||||
hlsPlayer.loadSource(hlsUrl.value)
|
||||
hlsPlayer.attachMedia(hlsVideoRef.value)
|
||||
|
||||
hlsPlayer.on(Hls.Events.MANIFEST_PARSED, () => {
|
||||
console.log('HLS manifest 解析成功')
|
||||
hlsVideoRef.value?.play()
|
||||
isHlsPlaying.value = true
|
||||
testHlsError.value = ''
|
||||
})
|
||||
|
||||
hlsPlayer.on(Hls.Events.ERROR, (event, data) => {
|
||||
console.error('HLS 错误:', data)
|
||||
|
||||
if (data.fatal) {
|
||||
testHlsError.value = `播放错误: ${data.type} - ${data.details}`
|
||||
isHlsPlaying.value = false
|
||||
switch (data.type) {
|
||||
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) {
|
||||
|
|
@ -202,6 +255,10 @@ const playHls = () => {
|
|||
}
|
||||
|
||||
const stopHls = () => {
|
||||
if (hlsRetryTimer) {
|
||||
clearTimeout(hlsRetryTimer)
|
||||
hlsRetryTimer = null
|
||||
}
|
||||
if (hlsPlayer) {
|
||||
hlsPlayer.destroy()
|
||||
hlsPlayer = null
|
||||
|
|
@ -210,6 +267,7 @@ const stopHls = () => {
|
|||
hlsVideoRef.value.pause()
|
||||
hlsVideoRef.value.src = ''
|
||||
}
|
||||
hlsRetryCount = 0
|
||||
isHlsPlaying.value = false
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue