Failed to construct 'AudioContext': number of hardware contexts reached maximum

谁说胖子不能爱 提交于 2019-12-03 15:22:25

问题


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.


回答1:


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?




回答2:


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




回答3:


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




回答4:


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.



来源:https://stackoverflow.com/questions/25046470/failed-to-construct-audiocontext-number-of-hardware-contexts-reached-maximum

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!