优化:公屏预加载叫号音频素材

需求:页面打开后缓存音频片段,叫号时避免临时下载造成延迟。

实现:页面挂载时预加载全部数字与提示语 WAV,后端继续返回叫号数据/文本,前端按文本映射片段播放。

验证:前端 66 项测试通过,生产构建通过。
This commit is contained in:
2026-08-01 12:06:01 +08:00
parent 01d261e893
commit 6d07612bc9
2 changed files with 41 additions and 2 deletions

View File

@@ -1,5 +1,10 @@
import { describe, expect, it } from "vitest";
import { announcementAudioQueue } from "./useAudioAnnouncements";
import { renderHook } from "@testing-library/react";
import { afterEach, describe, expect, it } from "vitest";
import { announcementAudioQueue, audioPreloadSources, useAudioAnnouncements } from "./useAudioAnnouncements";
afterEach(() => {
document.head.querySelectorAll("link[data-queue-voice-preload]").forEach((link) => link.remove());
});
describe("announcementAudioQueue", () => {
it("忽略项目名并把单号码播报素材重复三遍", () => {
@@ -34,4 +39,15 @@ describe("announcementAudioQueue", () => {
"/audio/queue-voice/entrance.wav",
]);
});
it("打开页面时预加载全部声音素材", () => {
const { unmount } = renderHook(() => useAudioAnnouncements());
const links = Array.from(document.head.querySelectorAll<HTMLLinkElement>("link[data-queue-voice-preload]"));
expect(links.map((link) => link.getAttribute("href"))).toEqual(audioPreloadSources);
expect(links.every((link) => link.rel === "preload" && link.getAttribute("as") === "audio")).toBe(true);
unmount();
expect(document.head.querySelector("link[data-queue-voice-preload]")).toBeNull();
});
});