feat: 正确向浏览器回报播放状态

This commit is contained in:
Astrian Zheng 2025-08-22 19:19:30 +10:00
parent d63e18f0c7
commit 03cd58b944
Signed by: Astrian
SSH Key Fingerprint: SHA256:rVnhx3DAKjujCwWE13aDl7uV6+9U1MvydLkNRXJrBiA

View File

@ -145,25 +145,38 @@ class WebAudioPlayer {
play() {
if (!playQueue.currentTrack) return
if (!playState.actualPlaying) {
//
debugPlayer("开始播放")
navigator.mediaSession.playbackState = 'playing'
if (playState.playProgress !== 0) debugPlayer(`已经有所进度!${playState.playProgress}`)
this.currentSource = this.context.createBufferSource()
this.currentSource.buffer = this.audioBuffer[playQueue.currentTrack.song.cid]
this.currentSource.connect(this.context.destination)
this.currentSource.start(this.context.currentTime, playState.playProgress)
playState.reportActualPlaying(true)
this.reportProgress()
//
if (this.currentSource && playState.actualPlaying) {
debugPlayer("已经在播放中,跳过")
return
}
debugPlayer("开始播放")
if (playState.playProgress !== 0) debugPlayer(`已经有所进度!${playState.playProgress}`)
// dummyAudio
this.dummyAudio.currentTime = 0
this.dummyAudio.play().catch(e => console.warn('DummyAudio play failed:', e))
//
if ('mediaSession' in navigator) {
navigator.mediaSession.playbackState = 'playing'
}
this.currentSource = this.context.createBufferSource()
this.currentSource.buffer = this.audioBuffer[playQueue.currentTrack.song.cid]
this.currentSource.connect(this.context.destination)
this.currentSource.start(this.context.currentTime, playState.playProgress)
playState.reportActualPlaying(true)
this.reportProgress()
//
//
this.currentTrackStartTime = this.context.currentTime - playState.playProgress
if (playQueue.nextTrack && this.audioBuffer[playQueue.nextTrack.song.cid]) this.scheduleNextTrack()
//
if (this.currentSource) this.currentSource.onended = () => {
this.currentSource.onended = () => {
debugPlayer("当前歌曲播放结束")
if (!!this.reportInterval) {
//
@ -226,12 +239,24 @@ class WebAudioPlayer {
}
pause() {
navigator.mediaSession.playbackState = 'paused'
debugPlayer("尝试暂停播放")
debugPlayer(this.currentSource)
// dummyAudio
this.dummyAudio.pause()
//
if ('mediaSession' in navigator) {
navigator.mediaSession.playbackState = 'paused'
}
this.currentSource?.stop()
this.nextSource?.stop()
this.nextSource = null
//
this.currentSource = null
this.nextSource = null
playState.reportActualPlaying(false)
this.stopReportProgress()
}
@ -244,6 +269,10 @@ class WebAudioPlayer {
// 2.
if (!this.nextSource) {
//
this.dummyAudio.pause()
if ('mediaSession' in navigator) {
navigator.mediaSession.playbackState = 'none'
}
playState.reportActualPlaying(false)
playState.togglePlay(false)
return
@ -276,18 +305,30 @@ onMounted(() => {
playerInstance.value = new WebAudioPlayer()
})
watch(() => playQueue.currentTrack, () => {
//
watch(() => playQueue.currentTrack, (newTrack, oldTrack) => {
debugPlayer(`检测到当前播放曲目更新`)
navigator.mediaSession.playbackState = playState.isPlaying ? 'playing' : 'paused'
navigator.mediaSession.metadata = new MediaMetadata({
title: playQueue.currentTrack.song.name,
artist: artistsOrganize(playQueue.currentTrack.song.artistes ?? []),
album: playQueue.currentTrack.album?.name,
artwork: [
{ src: playQueue.currentTrack.album?.coverUrl ?? "", sizes: '500x500', type: 'image/png' },
]
})
playerInstance.value?.loadResourceAndPlay()
if (newTrack) {
//
if ('mediaSession' in navigator) {
navigator.mediaSession.playbackState = playState.isPlaying ? 'playing' : 'paused'
navigator.mediaSession.metadata = new MediaMetadata({
title: newTrack.song.name,
artist: artistsOrganize(newTrack.song.artistes ?? []),
album: newTrack.album?.name,
artwork: [
{ src: newTrack.album?.coverUrl ?? "", sizes: '500x500', type: 'image/png' },
]
})
}
// onTrackEnded
// loadResourceAndPlay
if (!playState.actualPlaying || !oldTrack) {
playerInstance.value?.loadResourceAndPlay()
}
}
})
watch(() => playState.isPlaying, () => {