This commit is contained in:
孙小云 2025-12-10 18:19:20 +08:00
parent 6888d46ea2
commit 9d8d5cb7eb
1 changed files with 46 additions and 0 deletions

View File

@ -300,8 +300,54 @@ const formatFileSize = (size: number | null) => {
return (size / 1024 / 1024 / 1024).toFixed(2) + ' GB' return (size / 1024 / 1024 / 1024).toFixed(2) + ' GB'
} }
//
const handleBeforeUnload = async () => {
stopPlay()
await stopPlaybackStream()
}
//
const handleVisibilityChange = async () => {
if (document.hidden && isPlaying.value) {
console.log('页面不可见,停止播放以节省资源')
//
await stopPlayComplete()
}
}
//
const handleOnline = () => {
console.log('网络已恢复')
}
const handleOffline = async () => {
console.log('网络已断开,停止播放')
if (isPlaying.value) {
// API
stopPlay()
playError.value = '网络连接已断开'
}
}
//
if (typeof window !== 'undefined') {
window.addEventListener('beforeunload', handleBeforeUnload)
document.addEventListener('visibilitychange', handleVisibilityChange)
window.addEventListener('online', handleOnline)
window.addEventListener('offline', handleOffline)
}
// //
onUnmounted(async () => { onUnmounted(async () => {
//
if (typeof window !== 'undefined') {
window.removeEventListener('beforeunload', handleBeforeUnload)
document.removeEventListener('visibilitychange', handleVisibilityChange)
window.removeEventListener('online', handleOnline)
window.removeEventListener('offline', handleOffline)
}
//
stopPlay() stopPlay()
await stopPlaybackStream() await stopPlaybackStream()
}) })