Why the audio can not auto play?

I create sound which was set to be auto-play, just after a button click event . There still a loudspeaker on the screen. The user has interact with the screen so I suppose there should be no loudspeaker. Why is that?
1

Thats’s a feature of most browsers to avoid auto playing sounds :sweat_smile: It’s a bit annoying for developers but as a user I appreciate not being blasted with loud ad sounds and such :rofl:

5 Likes

If you would perform forum search you’ll find a lot of similar topics with ready-to-use solutions.
Here is Chrome policy with explanations - Autoplay policy in Chrome - Chrome Developers

3 Likes

For XR mode this works:

const xrHelper = await scene.createDefaultXRExperienceAsync({
        floorMeshes: ground,
        // ...etc
})

const music = new Sound( 'Music', '<ABSOLUTE URL HERE>', scene, null )

xrHelper.baseExperience.sessionManager.onXRSessionInit.add(() => {
    music.play()
})

xrHelper.baseExperience.sessionManager.onXRSessionEnded.add(() => {
    music.stop()
})

Yeah , I know it’s browser’s policy. But as it said in the image below, I create the sound AFTER the user click a BabylonJS button which indicate the user has interacted with the browser so that I suppose the music should auto play. And it does sometime ago, but suddenly it doesn’t work.

Frustrating, isn’t it? I somehow recall @RaananW said they are working towards a new audio player (or was it video?). Not sure it will address this issue. But then, as @labris said, there are still workarounds you can use for the time being. A bit tedious to search and implement but I suppose that’s all there is.
And I actually do agree with @carolhmj comment. Sadly, some (bastard :grimacing:) people would just make an abuse of the autoplay.

I recommend trying to start playing the sound in response to the user clicking on an html button instead of a Babylon button. Because some browsers, like Chrome on Mac for instance, don’t allow audio in response to canvas clicks but do in response to eg html buttons or keyboard input.

4 Likes

Might be , will give it a try

Autoplay will forever be an issue…

@docEdub is working on the audio engine revamp, and I am sure that he will work on that issue as well. This is a WIP, you can follow the issue on our public github

1 Like

I found the reason why audio can not auto play. But I do not know the root reason. That is if I comment the code in the attached image, then the audio will be able to auto play, otherwise not.
1