startMic()#
functionoav-p5startMic()
Turns the microphone on and returns a promise that says whether it worked.
You rarely need it: reading micLevel and its friends already starts the microphone. Call it when you want to choose the moment (a button, the end of an intro) or know the answer: startMic().then((ok) => { … }). Calling it again while it is on, or while the browser is still asking, returns the same result.
Before you call it
Browsers want a click before audio plays: call it from mousePressed() or a button handler if setup() is too early. It works whatever the mic option is, except ?oav_mic=off, where nothing can start the microphone.
Returns
Promise<Boolean> Resolves to true once the microphone is on, false if it was blocked, is missing, or the mic option is off.
Notes
- After
stopMic(), onlystartMic()(or 🎤) turns it back on; readingmicLeveldoes not.
Examples
function setup() {
createCanvas(400, 300);
textAlign(CENTER, CENTER);
}
function mousePressed() {
startMic().then((ok) => console.log(ok ? 'microphone on' : 'no microphone'));
}
function draw() {
background(20);
fill(255);
text(micIsOn ? 'level ' + nf(micLevel, 1, 2) : 'click to start the microphone', width / 2, height / 2);
}