Skip to content

Commit aeab832

Browse files
committed
🐞 fix: 修复错误处理
1 parent f8d4e74 commit aeab832

3 files changed

Lines changed: 62 additions & 3 deletions

File tree

src/components/Player/MainAMLyric.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
:alignPosition="settingStore.lyricsScrollPosition === 'center' ? 0.5 : 0.2"
1717
:enableBlur="settingStore.lyricsBlur"
1818
:style="{
19-
'--amll-lyric-view-color': 'var(--main-cover-color)',
19+
'--amll-lyric-view-color': 'rgb(var(--main-cover-color))',
2020
'--amll-lyric-player-font-size': settingStore.lyricFontSize + 'px',
2121
'--ja-font-family':
2222
settingStore.japaneseLyricFont !== 'follow' ? settingStore.japaneseLyricFont : '',

src/utils/audioManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ class AudioManager {
407407
* 获取音频错误码
408408
* @returns 错误码
409409
*/
410-
private getErrorCode(): number {
410+
public getErrorCode(): number {
411411
if (!this.audioElement?.error) return 0;
412412

413413
// 参考 HTML Audio Element 错误码

src/utils/player.ts

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,10 @@ class Player {
219219
}
220220
} catch (e) {
221221
console.error("Player create failed", e);
222+
// 触发错误处理
223+
const errCode = audioManager.getErrorCode();
224+
await this.handlePlaybackError(errCode || undefined);
225+
throw e;
222226
}
223227
// 获取歌词数据
224228
lyricManager.handleLyric(id, path);
@@ -313,6 +317,47 @@ class Player {
313317
this.retryInfo = { songId: Number(currentSongId || 0), count: 0 };
314318
}
315319
this.retryInfo.count += 1;
320+
// 1:用户中止了加载,不进行重试
321+
if (errCode === 1) {
322+
console.log("⏸️ 用户中止播放,不进行重试");
323+
this.retryInfo.count = 0;
324+
return;
325+
}
326+
// 4:音频格式不被支持,直接跳到下一首
327+
if (errCode === 4) {
328+
console.error("❌ 音频格式不支持:", { songId: currentSongId, errorCode: errCode });
329+
this.retryInfo.count = 0;
330+
if (dataStore.playList.length > 1) {
331+
window.$message.error("音频格式不支持,已跳至下一首");
332+
await this.nextOrPrev("next");
333+
} else {
334+
window.$message.error("当前列表暂无可播放歌曲");
335+
this.cleanPlayList();
336+
}
337+
return;
338+
}
339+
// 3:解码错误,通常无法通过重试解决,减少重试次数
340+
if (errCode === 3) {
341+
if (this.retryInfo.count <= 1) {
342+
console.log("🔄 检测到解码错误,尝试重试:", { count: this.retryInfo.count });
343+
if (this.retryInfo.count === 1) {
344+
window.$message.info("播放出现问题,正在尝试恢复...");
345+
}
346+
await this.initPlayer(true, currentSeek);
347+
return;
348+
}
349+
// 解码错误重试 1 次后直接跳过
350+
console.error("❌ 解码错误,重试失败:", { songId: currentSongId, errorCode: errCode });
351+
this.retryInfo.count = 0;
352+
if (dataStore.playList.length > 1) {
353+
window.$message.error("音频解码失败,已跳至下一首");
354+
await this.nextOrPrev("next");
355+
} else {
356+
window.$message.error("当前列表暂无可播放歌曲");
357+
this.cleanPlayList();
358+
}
359+
return;
360+
}
316361
// 2:资源过期或临时网络错误(通常是长时间暂停导致URL过期)
317362
if (errCode === 2 && this.retryInfo.count <= 2) {
318363
console.log("🔄 检测到资源过期,重新获取播放地址并从原位置继续:", currentSeek);
@@ -322,7 +367,7 @@ class Player {
322367
// 其它错误:最多 3 次,首次重试从原位置开始
323368
if (this.retryInfo.count <= 3) {
324369
const seekPosition = this.retryInfo.count === 1 ? currentSeek : 0;
325-
console.log("🔄 播放出错,尝试重试:", { count: this.retryInfo.count, seekPosition });
370+
console.log("🔄 播放出错,尝试重试:", { count: this.retryInfo.count, seekPosition, errCode });
326371
// 只在第一次重试时显示提示,避免过于频繁
327372
if (this.retryInfo.count === 1) {
328373
window.$message.info("播放出现问题,正在尝试恢复...");
@@ -433,6 +478,13 @@ class Player {
433478
await this.parseLocalMusicInfo(path);
434479
} catch (err) {
435480
console.error("播放器初始化错误(本地):", err);
481+
// createPlayer 内部已触发 handlePlaybackError,这里只记录日志
482+
// 如果 createPlayer 没有触发错误处理,则手动触发
483+
const errCode = audioManager.getErrorCode();
484+
if (errCode === 0) {
485+
// 如果没有错误码,可能是其他类型的错误,触发通用错误处理
486+
await this.handlePlaybackError(undefined);
487+
}
436488
}
437489
}
438490
// 在线歌曲
@@ -495,6 +547,13 @@ class Player {
495547
await this.createPlayer(playerUrl, autoPlay, seek);
496548
} catch (err) {
497549
console.error("播放器初始化错误(在线):", err);
550+
// createPlayer 内部已触发 handlePlaybackError,这里只记录日志
551+
// 如果 createPlayer 没有触发错误处理,则手动触发
552+
const errCode = audioManager.getErrorCode();
553+
if (errCode === 0) {
554+
// 如果没有错误码,可能是其他类型的错误,触发通用错误处理
555+
await this.handlePlaybackError(undefined);
556+
}
498557
}
499558
}
500559
}

0 commit comments

Comments
 (0)