Pergunta

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++) {
      contexts[i] = new AudioContext();
      analyzers[i] = contexts[i].createAnalyser();
   }
}catch(e) {
   console.log(e);
   // too many contexts created -- how do I remove them?
}

I've tried this, but it doesn't let me create new contexts after the fact: analyzers.forEach(function(analyzer){analyzer.disconnect(analyzer.context.destination)})

I am using Chrome 36 on Ubuntu Linux 14.04.

Foi útil?

Solução

You should really only have one AudioContext in the page.

From the docs: "In most use cases, only a single AudioContext is used per document."

I can't really think of a reason why you'd ever need more than one. Is there a specific issue you've run into that caused you to create multiple contexts?

Outras dicas

AudioContext.close() will release the hardware of the context, but check that it is available only for recent versions of chrome and firefox. Not available for IE aparently. Check the documentation: https://developer.mozilla.org/en-US/docs/Web/API/AudioContext/close

Reference it to a variable i.e:
var myAudioCtx = new AudioContext();
Then destroy by calling
myAudioCtx.close();
That's it.

I was working on a media player widget which broke on Chrome (but not Firefox) as soon as more than five were embedded in a single page.

I discovered that you can create a single AudioContext and store it globally, then have each instance just use the shared AudioContext. It seems to work no differently - you can attach multiple buffers and multiple destinations and the audio all gets mixed together.

This might require more effort if you're using it for timing, but for playback of one or more audio streams generated on-the-fly with JS code, sharing a single AudioContext works fine.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top