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
17
src/main.ts
17
src/main.ts
|
@ -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();
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue