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

后端 未结 5 1833
名媛妹妹
名媛妹妹 2021-02-07 03:49

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++) {
            


        
5条回答
  •  自闭症患者
    2021-02-07 04:01

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

提交回复
热议问题