Search code examples
typescripthtml5-audiovolumephaser-framework

How to set volume in Phaser 3?


I have a problem with the video game that I am trying to create.

The problem that I have is that, the song that I have made this one very strong and looking at the documentation I have seen that can form the volume, have applied this way of forming it without any result. Not if I have explained correctly...

My code in typescript:

export class SampleScene extends Phaser.Scene {
private song: Phaser.Sound.BaseSound;
 public preload() {
   // TODO: Add your code
   this.load.audio('theme', 'assets/audio/sayrun.mp3');
 }
 public create() {
     // TODO Add your code.
   this.song = this.sound.add('theme');
   this.song.play('volume', {volume: 0.5});
 }
}

Solution

  • It sounds like you have a bunch of audio clips, but one of them is louder than the other. When you play the loud one you're trying to turn down the volume to compensate.

    But your current code is trying to play a volume asset at half volume.

    Since you always want this particular audio asset to play at half volume, I would recommend changing your code so that the volume is set when you define the asset, and then just play() it.

    public create() {
        this.song = this.sound.add('theme', {volume: 0.5});
        this.song.play();
    }