Skip to content

Commit f03ea10

Browse files
committed
✨ feat: 增加状态锁
1 parent e369f28 commit f03ea10

1 file changed

Lines changed: 19 additions & 4 deletions

File tree

src/core/player/PlayerController.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ class PlayerController {
9090
private readonly MAX_RETRY_COUNT = 3;
9191
/** 当前曲目重试信息(按歌曲维度) */
9292
private retryInfo: { songId: number | string; count: number } = { songId: 0, count: 0 };
93-
93+
/** 当前播放请求标识 */
94+
private currentRequestToken = 0;
9495
/** 连续跳过计数 */
9596
private failSkipCount = 0;
9697

@@ -112,6 +113,10 @@ class PlayerController {
112113
const songManager = useSongManager();
113114
const audioManager = useAudioManager();
114115

116+
// 生成新的请求标识
117+
this.currentRequestToken++;
118+
const requestToken = this.currentRequestToken;
119+
115120
const { autoPlay = true, seek = 0 } = options;
116121
// 要播放的歌曲对象
117122
const playSongData = getPlaySongData();
@@ -138,19 +143,29 @@ class PlayerController {
138143
statusStore.playLoading = true;
139144
// 获取音频源
140145
const audioSource = await songManager.getAudioSource(playSongData);
146+
if (requestToken !== this.currentRequestToken) {
147+
console.log(`🚫 [${playSongData.id}] 请求已过期,舍弃`);
148+
return;
149+
}
141150
if (!audioSource.url) throw new Error("AUDIO_SOURCE_EMPTY");
142151
console.log(`🎧 [${playSongData.id}] 最终播放信息:`, audioSource);
143152
// 更新音质和解锁状态
144153
statusStore.songQuality = audioSource.quality;
145154
statusStore.playUblock = audioSource.isUnlocked ?? false;
146155
// 执行底层播放
147156
await this.loadAndPlay(audioSource.url, autoPlay, seek);
157+
if (requestToken !== this.currentRequestToken) return;
148158
// 后置处理
149159
await this.afterPlaySetup(playSongData);
150160
} catch (error: any) {
151-
console.error("❌ 播放初始化失败:", error);
152-
// 触发错误处理流程
153-
await this.handlePlaybackError(error?.code || 0, seek);
161+
if (requestToken === this.currentRequestToken) {
162+
console.error("❌ 播放初始化失败:", error);
163+
await this.handlePlaybackError(error?.code || 0, options.seek || 0);
164+
}
165+
} finally {
166+
if (requestToken === this.currentRequestToken) {
167+
statusStore.playLoading = false;
168+
}
154169
}
155170
}
156171

0 commit comments

Comments
 (0)