# Audio context hell

**URL:** <https://forum.babylonjs.com/t/audio-context-hell/20495>\
**Category:** Questions\
**Created:** [May 2, 2021, 1:49am UTC](https://forum.babylonjs.com/t/audio-context-hell/20495 "2021-05-02T01:49:48Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![saitogroup](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/saitogroup/32/10624_2.png) [@saitogroup](https://forum.babylonjs.com/u/saitogroup)\
**Post date:** [May 2, 2021, 1:49am UTC](https://forum.babylonjs.com/t/audio-context-hell/20495/1 "2021-05-02T01:49:48Z")

</div>

OK friends; so, I’ve just gone through all of the audio documentation ([Audio | Babylon.js Documentation](https://doc.babylonjs.com/divingDeeper/audio/playingSoundsMusic))

All of these examples are in the playground; so, maybe it avoids audio context hell?

I’m now trying to port this very fancy demo ([Babylon.js - Web Audio Analyser demo](https://www.babylonjs.com/demos/audioanalyser/)) into VR and hack on it, the problem is I can’t find the full code anywhere, so I had to go into the inspector and start ripping things out, but for the life of me, I cannot figure out how to fire the script from that button that starts the audio engine (and so I’m losing it, lol).

---

<div class="post-metadata">

**Author:** ![inteja](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/inteja/32/22553_2.png) [@inteja](https://forum.babylonjs.com/u/inteja)\
**Post date:** [May 2, 2021, 1:59am UTC](https://forum.babylonjs.com/t/audio-context-hell/20495/2 "2021-05-02T01:59:27Z")

</div>

Can you clarify exactly what you mean by “audio context hell”? It’s unclear to me.

---

<div class="post-metadata">

**Author:** ![saitogroup](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/saitogroup/32/10624_2.png) [@saitogroup](https://forum.babylonjs.com/u/saitogroup)\
**Post date:** [May 2, 2021, 2:01am UTC](https://forum.babylonjs.com/t/audio-context-hell/20495/3 "2021-05-02T02:01:15Z")

</div>

“babylon.js:16 The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. [https://goo.gl/7K7WLu](https://goo.gl/7K7WLu)  
e.\_initializeAudioContext @ babylon.js:16” ← that’s the error message

Somehow, I think I’m supposed to connect the button below to some process that tells engine to start playing the song; and, I have no idea how to do that :\<

 ![button](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/c/cfc4354b3e082b69c02e1175518a15cdb46c8e01.jpeg)

---

<div class="post-metadata">

**Author:** ![inteja](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/inteja/32/22553_2.png) [@inteja](https://forum.babylonjs.com/u/inteja)\
**Post date:** [May 2, 2021, 2:04am UTC](https://forum.babylonjs.com/t/audio-context-hell/20495/4 "2021-05-02T02:04:12Z")

</div>

Yes, I think there’s a cross-browser constraint where you can’t automatically play audio or video on page load - it has to be done on user click or some other event.

That icon I think BJS just overlays to indicate audio engine state. In the past I’ve had to hide it using CSS as I didn’t know any other way.

---

<div class="post-metadata">

**Author:** ![saitogroup](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/saitogroup/32/10624_2.png) [@saitogroup](https://forum.babylonjs.com/u/saitogroup)\
**Post date:** [May 2, 2021, 2:04am UTC](https://forum.babylonjs.com/t/audio-context-hell/20495/5 "2021-05-02T02:04:51Z")

</div>

Word; but how do I get my music to play? I can’t figure out how to get it to play… and I don’t see in the demo where I’m taking this from, how the developer got the music to start when you click that icon?

In the playground, stuff just starts playing automatically [Babylon.js Playground](https://playground.babylonjs.com/#PCY1J)

---

<div class="post-metadata">

**Author:** ![inteja](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/inteja/32/22553_2.png) [@inteja](https://forum.babylonjs.com/u/inteja)\
**Post date:** [May 2, 2021, 2:06am UTC](https://forum.babylonjs.com/t/audio-context-hell/20495/6 "2021-05-02T02:06:50Z")

</div>

I think `Engine.audioEngine.audioContext?.resume();`

---

<div class="post-metadata">

**Author:** ![saitogroup](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/saitogroup/32/10624_2.png) [@saitogroup](https://forum.babylonjs.com/u/saitogroup)\
**Post date:** [May 2, 2021, 2:07am UTC](https://forum.babylonjs.com/t/audio-context-hell/20495/7 "2021-05-02T02:07:32Z")

</div>

> [@inteja](#):
>
> Engine.audioEngine.audioContext?.resume();

where in the code would you put this? after you create the music? I don’t see this in his code… (thanks for giving this a shot btw)

---

<div class="post-metadata">

**Author:** ![saitogroup](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/saitogroup/32/10624_2.png) [@saitogroup](https://forum.babylonjs.com/u/saitogroup)\
**Post date:** [May 2, 2021, 2:10am UTC](https://forum.babylonjs.com/t/audio-context-hell/20495/8 "2021-05-02T02:10:13Z")

</div>

well the error message goes away, but I don’t hear anything lol; never mind sorry, it’s there, it’s just there twice now.

you need a user interaction to hear the music, but I don’t know how to code that, and I don’t see where else that is coded

---

<div class="post-metadata">

**Author:** ![inteja](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/inteja/32/22553_2.png) [@inteja](https://forum.babylonjs.com/u/inteja)\
**Post date:** [May 2, 2021, 2:13am UTC](https://forum.babylonjs.com/t/audio-context-hell/20495/9 "2021-05-02T02:13:44Z")

</div>

I can’t comment on how the PG works or the demo code, but assume there’s a bunch of setup going on behind the scenes in both cases.

In my own projects I’ve just ensured I call `Engine.audioEngine.audioContext?.resume();` on some user event, like clicking a play button.

You might also need to check `Engine.audioEngine.setGlobalVolume(1);`

And of course play your music with `myMusic.play()`

---

<div class="post-metadata">

**Author:** ![saitogroup](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/saitogroup/32/10624_2.png) [@saitogroup](https://forum.babylonjs.com/u/saitogroup)\
**Post date:** [May 2, 2021, 2:33am UTC](https://forum.babylonjs.com/t/audio-context-hell/20495/10 "2021-05-02T02:33:26Z")

</div>

I think I need to see a full example of audio working not on the playground.

I just tried to implement the simplest playground example of audio from the documentation and I couldn’t get it to play on [glitch.com](http://glitch.com)

So, somewhere somebody has a simple example that starts the audio context. e.g. this doesn’t work for me:

```
window.addEventListener("keydown", function(evt) {
          // Press space key to fire
          if (evt.keyCode === 32) {
            music.play();
            BABYLON.Engine.audioEngine.audioContext?.resume();
          }
        });
         BABYLON.Engine.audioEngine.setGlobalVolume(1);
```

---

<div class="post-metadata">

**Author:** ![inteja](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/inteja/32/22553_2.png) [@inteja](https://forum.babylonjs.com/u/inteja)\
**Post date:** [May 2, 2021, 4:17am UTC](https://forum.babylonjs.com/t/audio-context-hell/20495/11 "2021-05-02T04:17:10Z")

</div>

I made a [codepen](https://codepen.io/inteja/pen/bGgPPVa). It seems to work OK for me.

Click the sphere to play/pause the music.

Note that this also hides the default BJS unmute icon.

---

<div class="post-metadata">

**Author:** ![saitogroup](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/saitogroup/32/10624_2.png) [@saitogroup](https://forum.babylonjs.com/u/saitogroup)\
**Post date:** [May 2, 2021, 4:38am UTC](https://forum.babylonjs.com/t/audio-context-hell/20495/12 "2021-05-02T04:38:36Z")

</div>

I did an implementation of what you did; fixed a quick in glitch, and now everything works! THANK YOU!

---

<div class="post-metadata">

**Author:** ![saitogroup](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/saitogroup/32/10624_2.png) [@saitogroup](https://forum.babylonjs.com/u/saitogroup)\
**Post date:** [May 2, 2021, 5:29am UTC](https://forum.babylonjs.com/t/audio-context-hell/20495/13 "2021-05-02T05:29:45Z")

</div>

take a look at your work: [https://www.instagram.com/p/COW9w3JlViq/](https://www.instagram.com/p/COW9w3JlViq/)

audio is a little choppy, I’ll post about that tomorrow

---

<div class="post-metadata">

**Author:** ![arcman7](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/arcman7/32/20249_2.png) [@arcman7](https://forum.babylonjs.com/u/arcman7)\
**Post date:** [February 19, 2022, 8:18pm UTC](https://forum.babylonjs.com/t/audio-context-hell/20495/14 "2022-02-19T20:18:27Z")

</div>

This thread was helpful for me and I’ll just add my own react-based solution here:

```auto
  // Set up and enable the audio element component
  useEffect(() => {
    //@ts-ignore
    const audioEl: HTMLAudioElement = document.getElementById('musicAudioElement')
    setMusicAudioElement(audioEl)

    const listenForActivity = (): void => {
      console.log('trying to play after user activiy')
      audioEl?.play().catch(waitForUserActivity)
      document.removeEventListener('click', listenForActivity)
      Engine.audioEngine.audioContext?.resume()
      Engine.audioEngine.useCustomUnlockedButton = true
    }

    const waitForUserActivity = (e: any): void => {
      console.error('audioEl.play().catch', e)
      document.addEventListener('click', listenForActivity)
    }

    audioEl.play().catch(waitForUserActivity)
    //@ts-ignore
    audioEl.muted = false
  }, [])

```
