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