Skip to content

Audio set to loop infinitely stops, when next loop triggers while switched to another tab. #6702

@michalfialadev

Description

@michalfialadev

Version

  • Phaser Version: 3.60.0
  • Operating system: Win11
  • Browser: any / all (tested: Firefox Win11, Chrome Win11, Android, MacOS Safari)

Description

Posting following issue report as an info dump without source attached, cant create a repro due to time constraints (maybe later, seeing as i have 30days before closure..). Was able to work around the issue (@see below). Searched first, couldnt find a similar report, so here goes..

Im making a game, where if browser window goes out of focus, music mutes (not sfx, just music). For playing music only, i use separate 'audio' scene, which, when focus is lost, calls this.audioScene.sound.mute = true; and false, when focus is regained. Also i call this.audioScene.sound.pauseOnBlur = false; because the default focus handling / audio mute/unmute didn't always work as expected (all browsers/OSes..). So i funnel all the events (BLUR, FOCUS, HIDDEN, VISIBLE, PAUSE, RESUME) into 2 exit points onFocusGained and onFocusLost and handle the scene audio muting manually there.

Everything works wonders. Music is set to loop, loosing focus mutes it, regain of focus unmutes, works on all browsers/systems, all good. Except one strange issue - and this might be possibly unrelated to the muting code, it is likely related to the gain/loss of focus and how looping is handled internally by phaser during such situation (loss of focus as result of being switched to another tab during a loop trigger).

The issue is this: if i wait for a 1minute long infinitely looping music track to reach its end, and 5 seconds before it reaches its end (and Phaser makes the next loop of the music play), when i switch to another tab on FF/Chrome/MacOS Safari/whatever (this is 5 seconds before the end of the track, which is set to loop infinitely), and come back 10 seconds later (5 seconds after it should have been made to loop to the next loop), then the music unmutes and plays again (good), but 1 minute later, when the next loop should be started by Phaser, it wont start the next loop, and the music instead stops when the current loop finishes. This is unrelated to scene's audio's mute, since i can simply call resume() on the music track and it will start again; it simply will not be auto looped to next loop by phaser if it finishes its current loop. This auto looping bug only happens, when another tab is focused (game looses focus), while the next loop should start (and even then, it just stop looping after current music loop ends).

To work around this, i just opted to use pause/resume on currently playing music track, instead of scene.audio.mute {false|true}, when in|out of focus, that seems to 'work-around' well.

Example Test Code

WIP or none

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions