[][src]Static p5_sys::global::userStartAudio

pub static userStartAudio: UserStartAudioInternalType

It is not only a good practice to give users control over starting audio. This policy is enforced by many web browsers, including iOS and Google Chrome, which create the Web Audio API's Audio Context in a suspended state.

In these browser-specific policies, sound will not play until a user interaction event (i.e. mousePressed()) explicitly resumes the AudioContext, or starts an audio node. This can be accomplished by calling start() on a p5.Oscillator, play() on a p5.SoundFile, or simply userStartAudio().

userStartAudio() starts the AudioContext on a user gesture. The default behavior will enable audio on any mouseUp or touchEnd event. It can also be placed in a specific interaction function, such as mousePressed() as in the example below. This method utilizes StartAudioContext , a library by Yotam Mann (MIT Licence, 2016).

Examples

function setup() {
  // mimics the autoplay policy
  getAudioContext().suspend();

  let mySynth = new p5.MonoSynth();

  // This won't play until the context has resumed
  mySynth.play('A6');
}
function draw() {
  background(220);
  textAlign(CENTER, CENTER);
  text(getAudioContext().state, width/2, height/2);
}
function mousePressed() {
  userStartAudio();
}

Parameters

element(s)? This argument can be an Element, Selector String, NodeList, p5.Element, jQuery Element, or an Array of any of those.

callback? Callback to invoke when the AudioContext has started