I am trying to get audio capture from the microphone working on Safari on iOS11 after support was recently added
However, the onaudioprocess
callback is
There are two problems. The main one is that Safari on iOS 11 seems to automatically suspend new AudioContext
's that aren't created in response to a tap. You can resume()
them, but only in response to a tap.
(Update: Chrome mobile also does this, and Chrome desktop will have the same limitation starting in version 70 / December 2018.)
So, you have to either create it before you get the MediaStream
, or else get the user to tap again later.
The other issue with your code is that AudioContext
is prefixed as webkitAudioContext
in Safari.
Here's a working version:
(You can set the onaudioprocess
callback sooner, but then you get empty buffers until the user approves of microphone access.)
Oh, and one other iOS bug to watch out for: the Safari on iPod touch (as of iOS 12.1.1) reports that it does not have a microphone (it does). So, getUserMedia will incorrectly reject with an Error: Invalid constraint
if you ask for audio there.
FYI: I maintain the microphone-stream package on npm that does this for you and provides the audio in a Node.js-style ReadableStream. It includes this fix, if you or anyone else would prefer to use that over the raw code.