Web Audio API do not play after user interaction

I know that there is a limitation in iOS Safari where the audio is not playing until user triggers an interaction. So I have placed the code inside a

touchstart
event, but that does not cause the API to start playing.


I have also seen other questions in StackOverflow regarding this issue, and I have also tried a bunch of other tweaks including:

  • putting the audio load outside the
    touchstart
    callback
  • try adding a gain node
  • use 0.01 as the start time


and none of the above works in iOS Safari, but the audio plays OK in desktop Chrome and Safari. I can confirm the audio source has loaded fine, I can also confirm that the code has executed the play step, but nothing gets played.


Here is the link to the gist, you can see the versions where I made the changes (P.S. the click event is used for testing on desktop)

https://gist.github.com/angelathewebdev/32e0fbd817410db5dea1

Been 5 Years, no one at Apple who can address this? Its kinda of a shame that there are still so many issues with AudioContext on ios in 2020

I have understood the problem is casued because IPhone mute mode is on (controlled by IPhone's side switch). <Audio> tag works properly even the switch is set to "muted", however createBufferSource and createOscillator fail at the same time. Also, you can find another feature is the sound works on the earphone, but the speaker does not. For anyone who meets the same problem, try methods above to figure it out.

This took a little tracking-down, but it might be what you're missing:

This WebKit blog entry implies that certain "activation-triggering user events" will grant "user activation," which the Web Audio API requires on iOS. But "touchstart" is not one of those qualifying events.

The entry quotes the HTML spec as defining the following events as qualifying:

  • keydown, excluding the Escape key and possibly some keys reserved by the browser or OS
  • mousedown
  • pointerdown, but the pointerType must be “mouse”
  • pointerup, so long as the pointerType is not “mouse”
  • touchend
Web Audio API do not play after user interaction
 
 
Q