Can't get Web Audio API to work with iOS 11 Safari

So iOS 11 Safari was supposed to add support for the Web Audio API, but it still doesn't seem to work with this javascript code:

//called on page load
get_user_media = get_user_media || navigator.webkitGetUserMedia;  
get_user_media = get_user_media || navigator.mozGetUserMedia;
get_user_media.call(navigator, { "audio": true }, use_stream, function () { });
function use_stream(stream){
    var audio_context = new AudioContext();
    var microphone = audio_context.createMediaStreamSource(stream);
    window.source = microphone; // Workaround for https://bugzilla.mozilla.org/show_bug.cgi?id=934512
    var script_processor = audio_context.createScriptProcessor(1024, 1, 1);
    script_processor.connect(audio_context.destination);
    microphone.connect(script_processor);
    //do more stuff which involves processing the data from user's microphone...
}

I copy pasted most of this code, so I only have a cursory understanding of it. I know that it's supposed to (and does, on other browsers) capture the user's microphone for further processing. I know that the code breaks on the var audio_context = new AudioContext(); line (as in, no code after that is run), but don't have any error messages cause I don't have a mac which is required to debug iOS Safari (apple die already >_<) Anyone know what's going on and/or how to fix it?

e: forgot to mention that I looked it up and apparently I need the keyword "webkit" before using Web Audio API in Safari, but making it var audio_context = new webkitAudioContext(); doesn't work either

Upvotes: 6

Views: 16671

Answers (3)

Jonathan
Jonathan

Reputation: 1409

Audio won't play until an user interaction has occured, at which point you need to resume your AudioContext e.g.:

...
<button>Play</button>
...

<script>
  ...
  const c = new AudioContext
  ...
  document.querySelector('button').addEventListener('click',_=>c.resume())
</script>

Upvotes: 0

Kerry Davis
Kerry Davis

Reputation: 171

Nothing works on mobile save to home screen apps. I issued a bug report to Apple developer. Got a response that it was a duplicate ( which means they know..no clue if or when they will actually fix it).

Upvotes: 1

Nathan Friedly
Nathan Friedly

Reputation: 8146

@TomW was on the right track - basically the webkitAudioContext is suspended unless it's created in direct response to the user's tap (before you get the stream).

See my answer at https://stackoverflow.com/a/46534088/933879 for more details and a working example.

Upvotes: 10

Related Questions