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
Syntax
userStartAudio([elements], [callback])
Parameters
This argument can be an Element, Selector String, NodeList, p5.Element, jQuery Element, or an Array of any of those.
Callback to invoke when the AudioContext has started