Jump to content

Managing multiple sounds in just one Phaser sound object


Fenopiù
 Share

Recommended Posts

Good morning!

I've a related sounds question.

I would like to manage all my sounds in just one Phaser sound object, when i want to start playing one or more sounds, it all works properly but, when I have to check if one specific sound is playing, or if I want to pause/stop one specific sound I don't get how to do that.

Example:

let sounds: Phaser.Sound = null;
sounds = game.sound.play(sound1, 1, true);
sounds = game.sound.play(sound2, 1, false);
sounds = game.sound.play(sound3, 1, true);
sounds = game.sound.play(sound4, 1, false);

Now how I can check if sound2 is playing or pause/stop just sound4?

Link to comment
Share on other sites

On 15/12/2017 at 11:32 PM, samme said:

@samme all the examples have the same different case, I've already look at them before asking.

I have multiple sounds in my "sounds" object, in the examples any sound have his own object.

As there is a sounds.play(sound1) method, why there is not a sounds.isPlaying(sound1), a sounds.pause(sound1) and a sounds.stop(sound1)?

Or there is an way to do the same thing?

Link to comment
Share on other sites

I don't understand what you're trying to do. You don't have multiple sounds in the sounds variable. You're just overwriting it with one sound at a time.

You need to save a reference to each sound if you want to control them separately.

var sound1 = game.sound.play('key1', 1, true);
var sound2 = game.sound.play('key2', 1, false);
var sound3 = game.sound.play('key3', 1, true);
var sound4 = game.sound.play('key4', 1, false);

sound1.isPlaying;
sound2.paused = true;
sound3.stop();
sound4.restart();

 

Link to comment
Share on other sites

I'm trying to have just one function to manage the sound.

I give to this function the sound to play/pause/stop/resume, the volume and if it have to repeat the sound forever.

How can I manage that if, like you said, I need to have different variables?

Only with a switch/case or there is a better solution?

Link to comment
Share on other sites

37 minutes ago, Str1ngS said:

Have you also checked the audio sprite exampels? Because it sounds exactly like what you do/want/need.

http://phaser.io/examples/v2/audio/audio-sprite

Audio sprite example show just the play function (that already is the only to work)... and it have all sounds in just one file, I've about 20 sound files.

7 hours ago, samme said:

You can index the sounds by key yourself. Or you can loop through game.sound._sounds and find them.

Indexing the sounds by key is not what I've already done? O_o

For looping through game_sound._sounds I've to extend game.sound?

Link to comment
Share on other sites

2 hours ago, Fenopiù said:

Indexing the sounds by key is not what I've already done? O_o

var sounds = {
  key1: game.add.sound('key1'),
  key2: game.add.sound('key2'),
  key3: game.add.sound('key3')
};

sounds.key1.paused = true;
2 hours ago, Fenopiù said:

For looping through game_sound._sounds I've to extend game.sound?

No, any method with a reference to game.sounds could do this.

Link to comment
Share on other sites

16 hours ago, samme said:

var sounds = {
  key1: game.add.sound('key1'),
  key2: game.add.sound('key2'),
  key3: game.add.sound('key3')
};

sounds.key1.paused = true;

 

 

I've implemented this solution in two different ways:

const sounds_jukebox =
    {
        key1: game.add.sound('key1'),
        key2: game.add.sound('key2'),
        key3: game.add.sound('Sound/key3.wav')
    }
sounds_jukebox.key1.play();
sounds_jukebox.key2.isPlaying();
sounds_jukebox.key3.stop();
const sounds_jukebox2: { sound_id: string, sound: Phaser.Sound }[] =
    [
        {   "sound_id": "key1", "sound": game.add.sound('key1')},
        {   "sound_id": "key2", "sound": game.add.sound('key2')},
        {   "sound_id": "key3", "sound": game.add.sound('Sounds/key3.wav')}
    ]
sounds_jukebox2[key1].sound.play();
sounds_jukebox2[key2].sound.isPlaying();
sounds_jukebox2[key3].sound.stop();

But Google Chrome console log always give me this result:

Uncaught TypeError: Cannot read property 'keyN' of undefined.

I've even tryed to change "const" in "let" or "var" but nothing is changed.

I preload audio files with a JSON in preload state, so "keyN" instead of "PathSoundKeyN" should work, am I right?

Link to comment
Share on other sites

I'm just a jerk... solution found!

The second one is the one it works... with a little correction.

let sounds_jukebox: { sound: Phaser.Sound }[];

function jukebox(): void
{
  sounds_jukebox =
    [
        {"sound": game.add.sound('key1')},
        {"sound": game.add.sound('key2')},
        {"sound": game.add.sound('key3')}
    ]
}

sounds_jukebox[key1].sound.play();
sounds_jukebox[key2].sound.isPlaying();
sounds_jukebox[key3].sound.stop();

So, placing a jukebox() in app.ts, game is defined when I call sounds_jukebox!

Thanks @samme, I own you a beer!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...