Avoid multiple sound instances

Hello,

I have 2 audio files in my app, and when user switches between section, its respective audio track will play.

But when switching in same section, dual audio is played. I tired to stop all the audio source before I play another one, but still multiple audio tracks are played.

I have attached part of my code below

    this.entity.sound.pause();
    // this.entity.sound.stop();
    console.log("Audio Stopped - I");
    
        this.pauseVideoTextures(1);

        this.case1Track = this.entity.sound.play('Case1');

        if(audioTime != null){
            this.case1Track.currentTime = audioTime;
        }
        
        this.isCase1AudioPlaying = true;
        this.isCase1VideoPlaying = false;
      

Thanks,
Good day

Hi @MTHC_Pulkit,

If I understand correctly what you want is to set the Overlap property to false, on all sounds slots of your sound component.

That way when a new sound will play, it will first stop any existing sound that is playing.

1 Like

Hello @Leonidas,

Overlap was set to false when I was testing the app.

Are you able to share your project or a sample repro project to take a look at?

1 Like

it could possibly be handled this way: Prevent a sound from starting repeatedly · Issue #4885 · playcanvas/engine · GitHub

2 Likes

Hello to anyone facing this issue, below is how I fixed it.

As I explained, I ran this.entity.sound.stop(); and this.entity.sound.play(slotName) everytime when I wanted to play/switch between sound slots, i.e if I wanted to play Case 2 sound slot and pause Case 1 sound slot, I stopped all sounds and created new instance for Case 2 when there was already an instance for Case 2.

So, instead of running this.entity.sound.stop(); every time I paused the sound component. And instead of creating new instance of sound, I created an instance once and later played/paused using that instance.

My assumption was when this.entity.sound.stop(); executed, it would stop all sounds and destroy all sound instances. And I was wrong.

Thank you @Leonidas and @mvaligursky for reaching out and helping me.

3 Likes