Audio when browser tab is inactive (Web)

            // Continues playing when changing tabs
            this.audio_source.playOneShot(this.clip1);

            // Doesn't play when changing tabs
            this.audio_source.clip = this.clip2;
            this.audio_source.play();

Can anyone explain how this behavior is configured?

  • play() is for something that you want comple control over. We can pause/stop/resume with this. Normally, we used this for background music.

  • playOneShot() is for playing something one-shot as the name implies. We can’t stop it once it started. So, normally, it’s for sound effects, short playback time but large number of simultaneous playback.

Thanks for the explanation, but all of the above is in the documentation. The point of the question is why playOneShot() plays when the browser tab is not active, while play() does not. I understand what playOneShot() is used for, but apparently it can also be used to play music even when the browser is minimized, because play() does not allow this and does not have the appropriate settings, the question is why?

For play(), they pause the clip while the app is inactive. That’s why the music is stopped once the app is inactive.

For playOneShot(), they didn’t implement such monitoring since it’s intended for playing “short” sound effects. They didn’t keep track of these clips’ states. Once started, they will continue playing until the end regardless of the app is active or not (here, I personally think, they should stop these clips as well while the app is inactive).

Why? as I already said, they are for different purposes. The background music should be paused when the app is inactive. It’s not a limitation by the browser. They purposely paused the sound which was started with play(). This is a business requirement, not a technical one.

Beside, you can’t readily-use playOneShot() for background music as it doesn’t even have a “loop” feature.