javascript - AudioContext not allowed to start in ToneJS (Chrome) - Stack Overflow

Using ToneJS in Chrome, I frequently get this error message: "The AudioContext was not allowed to

Using ToneJS in Chrome, I frequently get this error message: "The AudioContext was not allowed to start. It must be resume (or created) after a user gesture on the page."

For example, with the code below, I get this message every time I use ctrl-r or ctrl-f5 to refresh the page.

I can get it working again by typing Tone.context.resume() into the console, but that gets pretty repetitive. Why this is going on, and how can I stop it?

var keyToPitch = { "z":"C3", "s":"C#3", "x":"D3", "d":"D#3", "c":"E3", "v":"F3", "g":"F#3", "b":"G3", "h":"G#3", "n":"A3", "j":"A#3", "m":"B3", ",":"C4" }


var synth = new Tone.PolySynth(6, Tone.Synth, {
    "oscillator" : {
        "type": "sawtooth",
        "partials" : [0, 2, 3, 4],
        }
    }).toMaster();


 window.addEventListener('keydown', this.onkeydown)
 window.addEventListener('keyup', this.onkeyup)

function onkeydown(e){
   synth.triggerAttack(keyToPitch[e.key], Tone.context.currentTime)
}
function onkeyup(e){
    synth.triggerRelease(keyToPitch[e.key])
}

Using ToneJS in Chrome, I frequently get this error message: "The AudioContext was not allowed to start. It must be resume (or created) after a user gesture on the page."

For example, with the code below, I get this message every time I use ctrl-r or ctrl-f5 to refresh the page.

I can get it working again by typing Tone.context.resume() into the console, but that gets pretty repetitive. Why this is going on, and how can I stop it?

var keyToPitch = { "z":"C3", "s":"C#3", "x":"D3", "d":"D#3", "c":"E3", "v":"F3", "g":"F#3", "b":"G3", "h":"G#3", "n":"A3", "j":"A#3", "m":"B3", ",":"C4" }


var synth = new Tone.PolySynth(6, Tone.Synth, {
    "oscillator" : {
        "type": "sawtooth",
        "partials" : [0, 2, 3, 4],
        }
    }).toMaster();


 window.addEventListener('keydown', this.onkeydown)
 window.addEventListener('keyup', this.onkeyup)

function onkeydown(e){
   synth.triggerAttack(keyToPitch[e.key], Tone.context.currentTime)
}
function onkeyup(e){
    synth.triggerRelease(keyToPitch[e.key])
}
Share Improve this question edited May 10, 2018 at 22:37 drenl asked May 10, 2018 at 21:11 drenldrenl 1,3534 gold badges19 silver badges32 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

It appears that Chrome is instituting a policy to require user interaction to use AudioContext so that sites can't intrusively play audio without the user initiating it.

Fortunately, you are already using user input to trigger the audio via keydown and keyup events. Instead of calling Tone.context.resume() manually, you can hook up the events to initiate resume(), like this:

function onkeydown(e){
  Tone.context.resume().then(() => {
    synth.triggerAttack(keyToPitch[e.key], Tone.context.currentTime)
  });
}
function onkeyup(e){
  Tone.context.resume().then(() => {
    synth.triggerRelease(keyToPitch[e.key])
  });
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743740587a4499065.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信