Audio

Hi, I have one question about audio: when I use this keyword - this.sound.add(key), it works perfectly, but then I try to use new Phaser.Sound.HTML5AudioSound(scene.sound, key), declared audio plays but isPlaying property remains the same even when the sound completes,

also, it doesn’t trigger a complete event to change isPlaying property manually.

what is the problem?

Sounds need to be added to the manager.

image
Sound Manager automatically adds them to list and does other stuff on it.
do you have specific reason to create Sound object other than Phaser’s native way?

yes, I want to create custom properties for sound class.
this structure works fine but I cant change the volume by this way
image

I think BaseSound class is not used directly, so it’s better to extend either WebAudioSound or HTML5AudioSound.
Downside of this approach is that it pops out of Phaser’s environment and you have to create each sound “manually”.
because of the way it’s implemented in library, we can’t simply swap out Sound class in Sound Manager.
Therefore I’m thinking it’s best to extend SoundManager class, override .add method - create instance of the sound object you’ve created instead of regular one.
replace default SoundManager class with new manager before creating Phaser.Game instance.
also you can update global declarations if you’re using TS.

also aside from that: simple linear fading isn’t the most natural way for fading sound because of how humans perceive sound. more details here: sound - What's the exact curve for natural audio fading? - Sound Design Stack Exchange

1 Like

You can add sounds to the manager manually, if you follow the manager code.

But I have to say that Test class looks very wrong. playSound() is adding a second sound which isn’t the instance.

It really looks like all you need is

function fadeIn(scene, sound, fadeDuration, toVolume) {
  // …
}
1 Like