chore: cache audio file
code generated by ChatGPT
This commit is contained in:
parent
8581ccc8ac
commit
72eada2873
1 changed files with 12 additions and 5 deletions
13
src/main.ts
13
src/main.ts
|
@ -1,11 +1,18 @@
|
||||||
|
const audioCache: Record<string, AudioBuffer> = {};
|
||||||
|
|
||||||
const audioPlay = async (url: string) => {
|
const audioPlay = async (url: string) => {
|
||||||
|
if (!audioCache[url]) {
|
||||||
const context = new AudioContext();
|
const context = new AudioContext();
|
||||||
const source = context.createBufferSource();
|
|
||||||
const audioBuffer = await fetch(url)
|
const audioBuffer = await fetch(url)
|
||||||
.then((res) => res.arrayBuffer())
|
.then((res) => res.arrayBuffer())
|
||||||
.then((ArrayBuffer) => context.decodeAudioData(ArrayBuffer));
|
.then((arrayBuffer) => context.decodeAudioData(arrayBuffer));
|
||||||
|
|
||||||
source.buffer = audioBuffer;
|
audioCache[url] = audioBuffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
const context = new AudioContext();
|
||||||
|
const source = context.createBufferSource();
|
||||||
|
source.buffer = audioCache[url];
|
||||||
source.connect(context.destination);
|
source.connect(context.destination);
|
||||||
source.start();
|
source.start();
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue