Skip to content

Commit 10ef32c

Browse files
committed
🐞 fix: 统一错误码 & 原生播放器去除跳转等待
1 parent 4db08ca commit 10ef32c

4 files changed

Lines changed: 31 additions & 19 deletions

File tree

src/core/audio-player/AudioElementPlayer.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { AUDIO_EVENTS, BaseAudioPlayer, type AudioEventType } from "./BaseAudioPlayer";
1+
import {
2+
AUDIO_EVENTS,
3+
AudioErrorCode,
4+
BaseAudioPlayer,
5+
type AudioEventType,
6+
} from "./BaseAudioPlayer";
27

38
/**
49
* 基于 HTMLAudioElement 的播放器实现
@@ -76,7 +81,8 @@ export class AudioElementPlayer extends BaseAudioPlayer {
7681
this.isInternalSeeking = true;
7782
this.targetSeekTime = time;
7883

79-
await super.seek(time);
84+
this.cancelPendingPause();
85+
this.doSeek(time);
8086
}
8187

8288
/**
@@ -149,13 +155,13 @@ export class AudioElementPlayer extends BaseAudioPlayer {
149155
if (!this.audioElement.error) return 0;
150156
switch (this.audioElement.error.code) {
151157
case MediaError.MEDIA_ERR_ABORTED:
152-
return 1;
158+
return AudioErrorCode.ABORTED;
153159
case MediaError.MEDIA_ERR_NETWORK:
154-
return 2;
160+
return AudioErrorCode.NETWORK;
155161
case MediaError.MEDIA_ERR_DECODE:
156-
return 3;
162+
return AudioErrorCode.DECODE;
157163
case MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED:
158-
return 4;
164+
return AudioErrorCode.SRC_NOT_SUPPORTED;
159165
default:
160166
return 0;
161167
}

src/core/audio-player/BaseAudioPlayer.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ export type AudioEventMap = {
2828
: Event;
2929
};
3030

31+
export enum AudioErrorCode {
32+
/** 用户中止 */
33+
ABORTED = 1,
34+
/** 网络错误 */
35+
NETWORK = 2,
36+
/** 解码错误 */
37+
DECODE = 3,
38+
/** 格式不支持 */
39+
SRC_NOT_SUPPORTED = 4,
40+
/** DOMException: AbortError */
41+
DOM_ABORT = 20,
42+
}
43+
3144
const SEEK_FADE_TIME = 0.05;
3245

3346
/**
@@ -263,7 +276,6 @@ export abstract class BaseAudioPlayer extends EventTarget {
263276

264277
/**
265278
* 取消正在进行的暂停计划
266-
*
267279
* 用于在淡出期间点击了播放或跳转,取消之前的暂停指令
268280
*/
269281
protected cancelPendingPause() {

src/core/audio-player/ffmpeg-engine/FFmpegAudioPlayer.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AUDIO_EVENTS, BaseAudioPlayer } from "../BaseAudioPlayer";
1+
import { AUDIO_EVENTS, AudioErrorCode, BaseAudioPlayer } from "../BaseAudioPlayer";
22
import AudioWorker from "./audio.worker?worker";
33
import type { AudioMetadata, PlayerState, WorkerResponse } from "./types";
44

@@ -7,13 +7,6 @@ const HIGH_WATER_MARK = 30;
77
/** 缓冲区低水位标记(秒),低于此值恢复解码 */
88
const LOW_WATER_MARK = 10;
99

10-
// const ERR_ABORTED = 1;
11-
/** 网络错误码 */
12-
const ERR_NETWORK = 2;
13-
/** 解码错误码 */
14-
const ERR_DECODE = 3;
15-
// const ERR_SRC_NOT_SUPPORTED = 4;
16-
1710
/**
1811
* 基于 FFmpeg WASM 的音频播放器实现
1912
*
@@ -159,7 +152,7 @@ export class FFmpegAudioPlayer extends BaseAudioPlayer {
159152
}
160153
} catch (e) {
161154
this.cleanupLoadPromise();
162-
this.handleError((e as Error).message, ERR_NETWORK);
155+
this.handleError((e as Error).message, AudioErrorCode.NETWORK);
163156
reject(e);
164157
}
165158
};
@@ -348,7 +341,7 @@ export class FFmpegAudioPlayer extends BaseAudioPlayer {
348341
* @param msg 错误消息
349342
* @param code 错误码
350343
*/
351-
private handleError(msg: string, code: number = ERR_DECODE) {
344+
private handleError(msg: string, code: number = AudioErrorCode.DECODE) {
352345
console.error("[FFmpegAudioPlayer]", msg, code);
353346

354347
this._errorCode = code;
@@ -381,7 +374,7 @@ export class FFmpegAudioPlayer extends BaseAudioPlayer {
381374
this.metadataReject(new Error(resp.error));
382375
this.cleanupLoadPromise();
383376
}
384-
this.handleError(resp.error, ERR_DECODE);
377+
this.handleError(resp.error, AudioErrorCode.DECODE);
385378
break;
386379
case "METADATA":
387380
this.metadata = {

src/core/player/PlayerController.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import lastfmScrobbler from "@/utils/lastfmScrobbler";
1212
import { calculateProgress } from "@/utils/time";
1313
import { LyricLine } from "@applemusic-like-lyrics/lyric";
1414
import { DebouncedFunc, throttle } from "lodash-es";
15+
import { AudioErrorCode } from "@/core/audio-player/BaseAudioPlayer";
1516
import { useAudioManager } from "./AudioManager";
1617
import { useLyricManager } from "./LyricManager";
1718
import { mediaSessionManager } from "./MediaSessionManager";
@@ -454,7 +455,7 @@ class PlayerController {
454455
);
455456

456457
// 用户主动中止 (Code 1) 或 AbortError (Code 20) - 不重试
457-
if (errCode === 1 || errCode === 20) {
458+
if (errCode === AudioErrorCode.ABORTED || errCode === AudioErrorCode.DOM_ABORT) {
458459
statusStore.playLoading = false;
459460
this.retryInfo.count = 0;
460461
return;

0 commit comments

Comments
 (0)