Is it possible to have 2 sound managers?

Greetings!

I’m trying to figure out how to work with sounds properly in Phaser 3. My idea is to have 2 sound managers, one for music and another one for sound effects, so I can manage easily the audio of the game.
The thing is that I can’t find a way of doing this. I’ve tried the answer to this old topic: Audio management
But I can’t get it to work because the funcion “create” doesnt exist in Phaser.Sound.SoundManagerCreator.

As I found this really strange, I checked the repository and found this: Where is SoundManagerCreator.create? · Issue #4774 · photonstorm/phaser · GitHub

Here, the creators said that the “create” function shouldn’t be accesible, but it looks like everybody is using it and I don’t know how ^^’

Maybe it works in the Javascript version, I’m using Typescript (v3.55.2). Does anyone have a clue about this? 0.o

EDIT: It DOES work in the Javascript version, probably due the nature of the language (not a JS expert talking hehe).

Thank you!

It exists but it’s not in Phaser’s definitions, I guess. Try

// ts-ignore
game.music = Phaser.Sound.SoundManagerCreator.create(game);
1 Like

You just saved my life!!

I’m not familiar with Typescript and I didn’t know it could ignore code when compiling! For anyone interested I post the solution applied to my code.

When creating the Game object I created the two new SoundManager:

callbacks: {
    preBoot: function (game) {
        // @ts-ignore: Unreachable code error
        game.musicAudioManager = Phaser.Sound.SoundManagerCreator.create(game);
        // @ts-ignore: Unreachable code error
        game.soundEffectAudioManager = Phaser.Sound.SoundManagerCreator.create(game);
    }
},

Afterwards, in my base scene class, I instantiated an small class named “SoundService.ts” like this:

BaseScene.ts:

// Called from standard preload function
protected initServices(): void {
    // @ts-ignore: Unreachable code error
    this.soundService = new SoundService(this.game.musicAudioManager, this.game.soundAudioManager);
}

Here is the SoundService.ts:

class SoundService {

    public music: Phaser.Sound.BaseSoundManager;
    public effect: Phaser.Sound.BaseSoundManager;

    constructor(musicAudioManager, soundAudioManager) {
        this.music = musicAudioManager;
        this.effect = soundAudioManager;
    }
}

This way I can now use both managers without having to ignore every line of code where I use them. Example of usage from random scene (which inherits from BaseScene.ts):

let button = this.add.image(0, 0, 0);
button.setInteractive();
button.on("pointerdown", function () {
    this.soundService.effect.play('click');
}, this);

Thank you for your help @samme !!