fix(playback): retry and downgrade quality before reporting 无法播放

Two failure modes look identical to the user — 「无法播放(区域/版权限制)」 — but
need different handling, and both were hitting the same code path:

1. **Transient.** NetEase intermittently answers `url: null` for a song that
   resolves fine a moment later. Repeating the SAME request fixes it, but the
   user had to click play repeatedly to be that retry.

2. **Quality gap.** Measured against a real library: a track can be playable at
   192k ("higher") while BOTH 320k ("exhigh") and 128k ("standard") return
   `url: null`. So the ladder is not continuous and "just drop to the cheapest"
   would still fail — every level below the current one has to be tried.

`resolveAndPlay` now resolves through `bot/url-retry.ts`:

- Phase 1: `1 + config.playUrlRetries` attempts (default 2, clamped 0..5) at the
  configured quality, 250ms linear backoff between them.
- Phase 2: one attempt per lower level from `provider.getFallbackQualities()`
  (NetEase: exhigh → higher → standard), 120ms apart — switching level is a
  different request, not a re-poke of the same one.
- Cancel (stop / disconnect) is checked before every attempt, so the budget
  never retries into a dead connection.
- A thrown error is retried exactly like an empty result: a sidecar timeout
  looks identical to the user.
- Failure is logged once per track, not once per attempt.

Every queue path funnels into `resolveAndPlay` (web click, chat !play,
auto-advance, FM), so playlists and albums get the same budget for free — that
is where "click the playlist four times before it starts" came from.

`config.playUrlRetries` is exposed on GET/POST `/api/bot/settings`. A config
without the field falls back to 2 — retries are ON by default because the whole
point is to be invisible. Set it to 0 to restore the previous fail-fast path.
This commit is contained in:
jiayuLian committed 2026-11-08 17:30:02 +08:00
1 parent 17008ae52e
commit c2771ab9a7
8 files changed
+566 -5

No files matched your search

+29
View File
@@ -314,3 +314,32 @@ describe("NeteaseProvider.getArtistAllSongs (全部歌曲 paging)", () => {
expect(page.hasMore).toBe(false);
});
});
describe("NetEase quality fallback ladder", () => {
it("offers every lower level, best-first, for the default 320k setting", () => {
const p = new NeteaseProvider("http://x");
// Default quality is exhigh (320k) → 192k then 128k.
expect(p.getFallbackQualities()).toEqual(["higher", "standard"]);
});
// The real-world case that produced «点四五次才能播»: a track playable at
// 192k while BOTH 320k and 128k return null. Dropping only to the cheapest
// level would still fail, so 192k has to be in the list.
it("includes intermediate levels, not just the cheapest", () => {
const p = new NeteaseProvider("http://x");
p.setQuality("lossless");
expect(p.getFallbackQualities()).toEqual(["exhigh", "higher", "standard"]);
});
it("has nothing to offer once already at the lowest level", () => {
const p = new NeteaseProvider("http://x");
p.setQuality("standard");
expect(p.getFallbackQualities()).toEqual([]);
});
it("never proposes a level above the configured one", () => {
const p = new NeteaseProvider("http://x");
p.setQuality("higher");
expect(p.getFallbackQualities()).toEqual(["standard"]);
});
});
+18
View File
@@ -206,6 +206,24 @@ export class NeteaseProvider implements MusicProvider {
return { url, trialDuration: parseNeteaseTrial(item) };
}
/**
* Levels to fall back to when the configured one returns no URL.
*
* NetEase does not offer a continuous quality ladder per track: measured on a
* real library, a track can be available at "higher" (192k) while BOTH
* "exhigh" (320k) and "standard" (128k) answer `url: null`. So the caller must
* sweep every level BELOW the current one instead of assuming "just drop to
* the cheapest".
*/
getFallbackQualities(): string[] {
const order = NETEASE_QUALITY_LEVELS.map((l) => l.value as string);
const index = order.indexOf(this.quality);
if (index <= 0) return [];
// order is low→high, so everything before the current level reversed gives
// best→worst below it (exhigh → ["higher", "standard"]).
return order.slice(0, index).reverse();
}
async getSongDetail(songId: string): Promise<Song | null> {
const res = await this.api.get("/song/detail", {
params: { ids: songId, ...this.cookieParams },
+11
View File
@@ -134,6 +134,17 @@ export interface MusicProvider {
setCookie(cookie: string): void;
getCookie(): string;
getAuthStatus(): Promise<AuthStatus>;
/**
* Lower quality levels to try when the configured one has no URL, ordered
* best→worst.
*
* NetEase does not publish a uniform ladder: a track can be playable at 192k
* ("higher") while BOTH 320k ("exhigh") and 128k ("standard") return null —
* so the list has to contain every level below the current one, not just the
* cheapest. Sources whose quality is not a user-selectable knob (Bilibili,
* local files, Jellyfin) omit this and the caller keeps its single attempt.
*/
getFallbackQualities?(): string[];
getPersonalFm?(): Promise<Song[]>;
getDailyRecommendSongs?(): Promise<Song[]>;
getUserPlaylists?(): Promise<Playlist[]>;