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

前端 未结 3 694
故里飘歌
故里飘歌 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 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)

提交回复
热议问题