fix (client): client crashes when Web Audio API is disabled (close #10948)
This commit is contained in:
parent
e047a4aa60
commit
f629ad327d
1 changed files with 14 additions and 5 deletions
|
@ -1,14 +1,23 @@
|
|||
import { ColdDeviceStorage } from "@/store";
|
||||
|
||||
const ctx = new AudioContext();
|
||||
const cache = new Map<string, HTMLAudioElement>();
|
||||
let ctx: AudioContext | null;
|
||||
try {
|
||||
ctx = new AudioContext();
|
||||
} catch {
|
||||
ctx = null;
|
||||
}
|
||||
|
||||
const cache = new Map<string, AudioBuffer>();
|
||||
|
||||
export async function getAudio(
|
||||
file: string,
|
||||
useCache = true,
|
||||
): HTMLAudioElement {
|
||||
): Promise<AudioBuffer | null> {
|
||||
if (useCache && cache.has(file)) {
|
||||
return cache.get(file);
|
||||
return cache.get(file) ?? null;
|
||||
}
|
||||
if (ctx == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const response = await fetch(`/static-assets/sounds/${file}.mp3`);
|
||||
|
@ -39,7 +48,7 @@ export function play(type: string) {
|
|||
|
||||
export async function playFile(file: string, volume: number) {
|
||||
const masterVolume = ColdDeviceStorage.get("sound_masterVolume");
|
||||
if (masterVolume === 0 || volume === 0) {
|
||||
if (ctx == null || masterVolume === 0 || volume === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue