Merge branch 'audio' into 'develop'
fix: sound effects stop music playback in iOS Co-authored-by: osamu <46447427+sam-osamu@users.noreply.github.com> Closes #10804 See merge request firefish/firefish!10641
This commit is contained in:
commit
342166c01f
1 changed files with 26 additions and 11 deletions
|
@ -1,16 +1,25 @@
|
|||
import { ColdDeviceStorage } from "@/store";
|
||||
|
||||
const ctx = new AudioContext();
|
||||
const cache = new Map<string, HTMLAudioElement>();
|
||||
|
||||
export function getAudio(file: string, useCache = true): HTMLAudioElement {
|
||||
let audio: HTMLAudioElement;
|
||||
export async function getAudio(
|
||||
file: string,
|
||||
useCache = true,
|
||||
): HTMLAudioElement {
|
||||
if (useCache && cache.has(file)) {
|
||||
audio = cache.get(file);
|
||||
} else {
|
||||
audio = new Audio(`/static-assets/sounds/${file}.mp3`);
|
||||
if (useCache) cache.set(file, audio);
|
||||
return cache.get(file);
|
||||
}
|
||||
return audio;
|
||||
|
||||
const response = await fetch(`/static-assets/sounds/${file}.mp3`);
|
||||
const arrayBuffer = await response.arrayBuffer();
|
||||
const audioBuffer = await ctx.decodeAudioData(arrayBuffer);
|
||||
|
||||
if (useCache) {
|
||||
cache.set(file, audioBuffer);
|
||||
}
|
||||
|
||||
return audioBuffer;
|
||||
}
|
||||
|
||||
export function setVolume(
|
||||
|
@ -28,11 +37,17 @@ export function play(type: string) {
|
|||
playFile(sound.type, sound.volume);
|
||||
}
|
||||
|
||||
export function playFile(file: string, volume: number) {
|
||||
export async function playFile(file: string, volume: number) {
|
||||
const masterVolume = ColdDeviceStorage.get("sound_masterVolume");
|
||||
if (masterVolume === 0 || volume === 0 || file.toLowerCase().includes("none"))
|
||||
if (masterVolume === 0 || volume === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const audio = setVolume(getAudio(file), volume);
|
||||
audio.play();
|
||||
const gainNode = ctx.createGain();
|
||||
gainNode.gain.value = masterVolume * volume;
|
||||
|
||||
const soundSource = ctx.createBufferSource();
|
||||
soundSource.buffer = await getAudio(file);
|
||||
soundSource.connect(gainNode).connect(ctx.destination);
|
||||
soundSource.start();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue