Search code examples
iframeweb-audio-api

Starting an AudioContext in an iframe


I'm developing a web audio SDK and considering embedding just the audio (so not the UI elements) in an iframe. I'm getting the following error:

The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.

I'm wondering if there is a way to create a user gesture on the iframe even though it is hidden and audio-only? The original gesture is on the parent component, but it seems like that is not propagating down to the frame. Thanks!


Solution

  • For better user experience (UX), browsers disallow auto-playing of audio unless users explicitly "request" audio with a gestures like clicks/taps. The iFrame must be visible and interactive to receive user gestures, otherwise the UX safeguard is bypassed.