javascript 无法构建“AudioContext”:硬件上下文数量达到最大值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25046470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 03:47:51  来源:igfitidea点击:

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

javascriptweb-audio-api

提问by Ian Hunter

Is there a way to remove an AudioContextafter I've created it?

有没有办法AudioContext在我创建之后删除它?

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)})

我试过这个,但它不允许我在事后创建新的上下文: analyzers.forEach(function(analyzer){analyzer.disconnect(analyzer.context.destination)})

I am using Chrome 36 on Ubuntu Linux 14.04.

我在 Ubuntu Linux 14.04 上使用 Chrome 36。

采纳答案by Kevin Ennis

You should really only have one AudioContextin the page.

您真的应该AudioContext在页面中只有一个。

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

来自文档:“在大多数用例中,每个文档只使用一个 AudioContext。”

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

我真的想不出为什么你需要不止一个的原因。您是否遇到过导致您创建多个上下文的特定问题?

回答by Julian

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

AudioContext.close() 将释放上下文的硬件,但检查它是否仅适用于最新版本的 chrome 和 firefox。显然不适用于 IE。检查文档:https: //developer.mozilla.org/en-US/docs/Web/API/AudioContext/close

回答by Omar Attia

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

将它引用到一个变量,即:
var myAudioCtx = new AudioContext();
然后通过调用它来销毁
myAudioCtx.close();

回答by Malvineous

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.

我正在开发一个媒体播放器小部件,一旦在单个页面中嵌入了五个以上,它就会在 Chrome(但不是 Firefox)上损坏。

I discovered that you can create a single AudioContextand 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.

我发现您可以创建一个AudioContext并全局存储它,然后让每个实例只使用共享的AudioContext. 它的工作方式似乎没有什么不同 - 您可以附加多个缓冲区和多个目的地,并且音频全部混合在一起。

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 AudioContextworks fine.

如果您将它用于计时,这可能需要更多的努力,但是对于使用 JS 代码即时生成的一个或多个音频流的播放,共享一个AudioContext可以正常工作。