phaserjs / phaser

Phaser is a fun, free and fast 2D game framework for making HTML5 games for desktop and mobile web browsers, supporting Canvas and WebGL rendering.
https://phaser.io
MIT License
36.91k stars 7.08k forks source link

Background Music (BGM) Fails to Resume After Closing Advertisements #6868

Closed cy920820 closed 1 month ago

cy920820 commented 1 month ago

Description

There is an issue with background music (BGM) not resuming correctly after closing advertisements in a Phaser 3 game embedded in a webview. When the app goes to the background, the BGM pauses as expected. However, when the app returns to the foreground and an advertisement (with sound) has been shown, the BGM does not resume after closing the advertisement.

Steps to Reproduce

  1. Start the Phaser 3 game with looping background music (BGM).
  2. Switch the app to the background, which pauses the BGM.
  3. Return to the app, triggering an advertisement with sound to play.
  4. Close the advertisement.
  5. Observe that the BGM does not resume.

Expected Behavior

The BGM should resume playing after the advertisement is closed and the app returns to the foreground.

Actual Behavior

The BGM remains paused and does not resume after the advertisement is closed and the app is back in the foreground.

Additional Information

function handleVisibilityChange(context) { if (document.hidden) { context.bgm.pause(); } else { if (!isAdPlaying()) { context.bgm.resume(); } else { onAdEnded(() => { context.bgm.resume(); }); } } }

function isAdPlaying() { const adAudio = document.getElementById('adAudio'); return adAudio && !adAudio.paused; }

function onAdEnded(callback) { const adAudio = document.getElementById('adAudio'); if (adAudio) { adAudio.addEventListener('ended', callback, { once: true }); } }



This issue seems to be related to the interaction between the audio contexts of the advertisement and the Phaser game. Any guidance or fixes would be greatly appreciated.

Thank you!
cy920820 commented 1 month ago

I suspect that Phaser failed to correctly restore the audio context, So what should we do in this situation?