Is there a way to remove an AudioContext
after I\'ve created it?
var analyzers = [];
var contexts = [];
try {
for(var i = 0; i<20; i++) {
Close it before each click. If you use the timer to delay the closing, there will be an exception of incomplete playback, especially when switching files.
const audioPlay = (() => {
let context = null;
return async () => {
if (context) context.close();
context = new AudioContext();
const source = context.createBufferSource();
source.buffer = await fetch('./2.mp3')
.then(res => res.arrayBuffer())
.then(arrayBuffer => context.decodeAudioData(arrayBuffer));
source.connect(context.destination);
source.start();
};
})();
document.querySelector('#add').addEventListener('click', audioPlay);
Not recommended
setTimeout(() => { context.close();}, 400);