feat: 优化播放检测逻辑

This commit is contained in:
algerkong
2025-12-20 14:16:32 +08:00
parent 77bb06c0d6
commit 999cd6526b
3 changed files with 37 additions and 90 deletions
+10 -6
View File
@@ -103,15 +103,18 @@ export const usePlayerCoreStore = defineStore(
/**
* 播放状态检测
*/
const checkPlaybackState = (song: SongResult, requestId: string, timeout: number = 4000) => {
const checkPlaybackState = (song: SongResult, requestId?: string, timeout: number = 4000) => {
if (checkPlayTime) {
clearTimeout(checkPlayTime);
}
const sound = audioService.getCurrentSound();
if (!sound) return;
// 如果没有提供 requestId,创建一个临时标识
const actualRequestId = requestId || `check_${Date.now()}`;
const onPlayHandler = () => {
console.log('播放事件触发,歌曲成功开始播放');
console.log(`[${actualRequestId}] 播放事件触发,歌曲成功开始播放`);
audioService.off('play', onPlayHandler);
audioService.off('playerror', onPlayErrorHandler);
};
@@ -121,13 +124,14 @@ export const usePlayerCoreStore = defineStore(
audioService.off('play', onPlayHandler);
audioService.off('playerror', onPlayErrorHandler);
// 验证请求是否仍然有效
if (!playbackRequestManager.isRequestValid(requestId)) {
// 如果有 requestId,验证其有效
if (requestId && !playbackRequestManager.isRequestValid(requestId)) {
console.log('请求已过期,跳过重试');
return;
}
if (userPlayIntent.value && play.value) {
console.log('播放失败,尝试刷新URL并重新播放');
playMusic.value.playMusicUrl = undefined;
const refreshedSong = { ...song, isFirstPlay: true };
await handlePlayMusic(refreshedSong, true);
@@ -138,8 +142,8 @@ export const usePlayerCoreStore = defineStore(
audioService.on('playerror', onPlayErrorHandler);
checkPlayTime = setTimeout(() => {
// 验证请求是否仍然有效
if (!playbackRequestManager.isRequestValid(requestId)) {
// 如果有 requestId,验证其有效
if (requestId && !playbackRequestManager.isRequestValid(requestId)) {
console.log('请求已过期,跳过超时重试');
audioService.off('play', onPlayHandler);
audioService.off('playerror', onPlayErrorHandler);
+12 -3
View File
@@ -398,7 +398,12 @@ export const usePlaylistStore = defineStore(
console.log(
`[nextPlay] 尝试播放: ${nextSong.name}, 索引: ${currentIndex} -> ${nowPlayListIndex}, 单曲重试: ${singleTrackRetryCount}/${SINGLE_TRACK_MAX_RETRIES}, 连续失败: ${consecutiveFailCount.value}/${MAX_CONSECUTIVE_FAILS}`
);
console.log('[nextPlay] Current mode:', playMode.value, 'Playlist length:', playList.value.length);
console.log(
'[nextPlay] Current mode:',
playMode.value,
'Playlist length:',
playList.value.length
);
// 先尝试播放歌曲
const success = await playerCore.handlePlayMusic(nextSong, true);
@@ -408,7 +413,10 @@ export const usePlaylistStore = defineStore(
consecutiveFailCount.value = 0;
playListIndex.value = nowPlayListIndex;
console.log(`[nextPlay] 播放成功,索引已更新为: ${nowPlayListIndex}`);
console.log('[nextPlay] New current song in list:', playList.value[playListIndex.value]?.name);
console.log(
'[nextPlay] New current song in list:',
playList.value[playListIndex.value]?.name
);
sleepTimerStore.handleSongChange();
} else {
console.error(`[nextPlay] 播放失败: ${nextSong.name}`);
@@ -568,7 +576,8 @@ export const usePlaylistStore = defineStore(
const sound = audioService.getCurrentSound();
if (sound) {
sound.play();
// checkPlaybackState 已在 playAudio 中自动调用,无需在这里重复调用
// 在恢复播放时也进行状态检测,防止URL已过期导致无声
playerCore.checkPlaybackState(playerCore.playMusic);
}
}
return;