(Web Audio API) Oscillator node error: cannot call start more than once

前端 未结 3 685
故里飘歌
故里飘歌 2020-12-15 17:16

When I start my oscillator, stop it, and then start it again; I get the following error:

Uncaught InvalidStateError: Failed to execute \'start\' on \'Oscillat         


        
相关标签:
3条回答
  • 2020-12-15 17:51

    The best solution I've found so far is to keep the SAME audioContext while recreating the oscillator every time you need to use it.

    http://jsfiddle.net/xbqbzgt2/3/

    FYI You can only create 6 audioContext objects per browser page lifespan (or at least per my hardware):

    Uncaught NotSupportedError: Failed to construct 'AudioContext': The number of hardware contexts provided (6) is greater than or equal to the maximum bound (6).
    
    0 讨论(0)
  • 2020-12-15 18:08

    A better way would be to start the oscillatorNode once and connect/disconnect the oscillatorNode from the graph when needed, ie :

    var ctx = new AudioContext();
    var osc = ctx.createOscillator();   
    osc.frequency.value = 8000;    
    osc.start();    
    $(document).ready(function() {
        $("#start").click(function() {
             osc.connect(ctx.destination);
        });
        $("#stop").click(function() {
             osc.disconnect(ctx.destination);
        });
    });
    

    This how muting in done in muting the thermin (mozilla web audio api documentation)

    0 讨论(0)
  • 2020-12-15 18:11

    From what I know, an oscillator can only be played once, for reasons having to do with precision, and never well explained by anyone yet. Whoever decided on this "play only once" model probably would consider it good practice to use a zero-volume setting to insert silence in the middle of a sequence. After all, it really is the only alternative to the disconnect-and-recreate method.

    0 讨论(0)
提交回复
热议问题