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

13,656

Solution 1

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

Solution 2

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?

Solution 3

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

Solution 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.

Share:
13,656

Related videos on Youtube

Ian Hunter
Author by

Ian Hunter

Updated on June 05, 2022

Comments

  • Ian Hunter
    Ian Hunter about 2 years

    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.

  • Ian Hunter
    Ian Hunter almost 10 years
    Is there a specific issue you've run into that caused you to create multiple contexts? — no, not really. I just assumed that because I could make multiple AudioContexts, there wouldn't be any issue. Until I reached 6, which is apparently as many as my hardware can handle. I can rework it so there's just one. Is there no way to destroy an instance of an AudioContext, though?
  • Kevin Ennis
    Kevin Ennis almost 10 years
    No, there's no way to destroy an AudioContext. It's possible that they can be garbage collected if nothing else contains a reference to them, but I'm not really sure if that's the case.
  • Ian Hunter
    Ian Hunter almost 10 years
    I've tried setting my references to the contexts to null, but it didn't seem to release the count of contexts that was causing that error. Maybe a bug? Or I missed something. Either way, keeping only one AudioContext (so far) has fixed my problem. Thank you for your help.
  • notthetup
    notthetup almost 10 years
    Also in most common use cases you want all your Nodes running in the same context, so that you can synchronize them if the need be. As for forcing GC of the AudioConext, you can't really know when it will get GCed. So setting it to null ensure it gets GCed at some point in time, but you never know exactly when.
  • samccone
    samccone over 9 years
    A use case @KevinEnnis is when you are using timed value transitions such as osc.detune.setValueAtTime(...) and you do not want to keep track of the current context's time.
  • Kevin Ennis
    Kevin Ennis over 9 years
    I guess I don't really understand what you mean. I'd have to see some code to contrast the approach you'd use with a single context vs the way you want to do it using multiple contexts.
  • Moonwalker
    Moonwalker over 8 years
    you got it right Julian! I started calling close() on the audio context instance created in my React component when component was getting unmounted and that resolved the AudioContext creation error. Thank...
  • Nek
    Nek over 7 years
    Keep in mind that total number of audio contexts is per browser instance, not per window. You can get out of luck if some other opened site/sites has/have already depleted the available pool. No real workaround but you should at least wrap audio context initialisation into try/catch and have some UI for error scenario.
  • Nek
    Nek over 7 years
    There is also AudioContext.suspend(). I needed this feature so much around 4 years ago. Glad to see the improvements though.
  • Simone Mazzoni
    Simone Mazzoni over 7 years
    @Julian Is there a way to check how many audioContext are opened in a page? Or do I have to keep track of them manually?
  • Jack G
    Jack G over 7 years
    This should be the answer. Julian is the real MVP.
  • Malvineous
    Malvineous over 6 years
    @KevinEnnis: To answer your question about why you'd need multiple contexts, I am working on a media player widget and as soon as someone embeds more than five widgets on a page they stop working (think of a list of album tracks with an audio preview next to each one.)
  • Kevin Ennis
    Kevin Ennis over 6 years
    Why not namespace your AudioContext for re-use? So, like, window.reusableAudioContextForMyMediaPlayer. When you instantiate a player, you look for that. If it exists, use it. Otherwise create it.
  • weiya ou
    weiya ou over 3 years
    Don't close it too fast, otherwise it will not play in time. setTimeout(() => { context.close();}, 400);
  • Brad
    Brad over 3 years
    A good reason for having a new audio context is for changing sample rates. If we're capturing from an audio device, we need to match its sample rate. If we change the device, the sample rate can change.