Stopping Sound using Attributes

The stop button does not work in the following code.

Code Editor - https://playcanvas.com/editor/code/679574?tabs=30117616,30253386,30253389,30129174,30253390,30253348

Editor - https://playcanvas.com/editor/scene/907428

Also I would like to make sure when any of the audio sounds are played. Whatever was playing before stops. Ex: if Jazz is playing and Country is clicked, then Jazz should stop.

Thank you,

Hi @Krish_USCan,

Try calling stop on the sound component of the entity, not the script instance:

this.entity.sound.stop('BluesSound');

To stop all sounds played on a sound component, just call the same method without providing any sound name as a parameter.

https://developer.playcanvas.com/en/api/pc.SoundComponent.html#stop

Oh that works thank you.

But is there a way to solve my second problem.
Do let me know if my explanation makes sense or not.

:slight_smile:

1 Like

Now if you are talking of stopping all sounds playing currently app-wide you will have to use a custom system for that. Leverage app events, on each entity holding sounds add an event like this:

this.app.on('sounds:stopAll', function(){
   this.entity.sound.stop();
}, this);

Now from any script in your app you can call the following event and all sounds playing will stop.

this.app.fire('sounds:stopAll');
2 Likes

Hi, this is an old topic, but I was wondering how you unpause/start the sound again.

Hi @Kyle_3_1415!

this.entity.sound.play('Name of slot');

Thanks, I was trying different stuff, but I never thought of ‘play’… :sweat_smile:

1 Like

You can store the instance of the sound in a variable and control the functions like play, pause and other things with the variable