chore: cache audio file

code generated by ChatGPT
This commit is contained in:
Guanran Wang 2024-11-17 17:55:45 +08:00
parent 8581ccc8ac
commit 72eada2873
Signed by: nyancat
GPG key ID: 91F97D9ED12639CF

View file

@ -1,11 +1,18 @@
const audioCache: Record<string, AudioBuffer> = {};
const audioPlay = async (url: string) => {
if (!audioCache[url]) {
const context = new AudioContext();
const audioBuffer = await fetch(url)
.then((res) => res.arrayBuffer())
.then((arrayBuffer) => context.decodeAudioData(arrayBuffer));
audioCache[url] = audioBuffer;
}
const context = new AudioContext();
const source = context.createBufferSource();
const audioBuffer = await fetch(url)
.then((res) => res.arrayBuffer())
.then((ArrayBuffer) => context.decodeAudioData(ArrayBuffer));
source.buffer = audioBuffer;
source.buffer = audioCache[url];
source.connect(context.destination);
source.start();
};