Home > Mobile >  How can you detect wether any music is playing in Phaser?
How can you detect wether any music is playing in Phaser?

Time:12-22

In my game, I want to play music sometimes. But music that is already playing shouldn’t be overridden.

So how can I

  1. Check if music is playing
  2. Manage that sound

Ideally in an global interface like this.sound

Thanks in advance

CodePudding user response:

After read your question I understand that you want to track the music.

The best solution would be like

  1. First play the music
  2. Store the id inside the a array
  3. Next time when you play the music check the id inside the array
  4. If the music id inside the array then play different music if not then play the music.

code example

let arr = [];

function playmusic(musicId){
  if ((!arr .includes(musicId)) {
 // your code 

 arr.push(musicId);
}

Take the idea from it and try.

CodePudding user response:

HTML5AudioSound has isPlaying member.

// inside scene method, create sound
const music = this.sound.add('music-name', {});
music.play();

// and later
if(music.isPlaying) {

}

See https://newdocs.phaser.io/docs/3.54.0/Phaser.Sound.HTML5AudioSound#isPlaying and https://newdocs.phaser.io/docs/3.52.0/Phaser.Sound.HTML5AudioSoundManager

(this.sound is HTML5AudioSoundManager instance)

  • Related