Make a playlist / audio triggering next audio after being played

Hi I’m very new to PlayCanvas and I dont know how to code.

I’d like to create an animation with a playlist for my music, basically kind of a radio station. Is this complicated? Basically what I am looking for is a way to let another sound play after another automatically as the background music of my animation.

Im trying these things here SoundComponent | PlayCanvas API Reference
but i’m not really sure if they are intended to trigger a component after another after being played?

As far as I understand I need to write a script that would trigger sound components after each other, right? Could someone help me with that or is there already one i can adapt from another game?

Thanks for any advice how to do this without much experience!


I think what you want to look for are the slots.

Anyway, did you try to start with the audio tutorials?

Hello @asnras, to do what you’re looking for, you’ll want to take a look at the SoundComponent, as you’ve mentioned. What I’d do is use the entity.sound.on('end' ... event to get a notification of when the current clip has finished playing.

If you’re interested, I’ve made an example project to showcase how this can be done. Take a look at the clip-player.js script, as well as the Play Button entity in the scene.

Wow thank you so much, this is really helpful!

Do you think there is a way to have the clips played in a random order? And loop the whole sequence endlessly?

Also I’m trying to find out how to rewrite the code so it does auto-play by default without a button.

Thanks in advance!!!

You should be able to make clips be played in a random order by shuffling the array of songs being played. You can do that with a simple algorithm, like Fisher-Yates Shuffle.

To loop the sequence endlessly you could just check if currentSongIndex is greater or equal to songList.length - 1. If it is, set currentSongIndex to 0, effectively restarting the playlist.

To auto-play by default, check the ‘Auto Play’ box in the SoundComponent.

Hey thanks so much,
I’m trying this but I don’t know really how where and how to implement that code into the clip-player.js script. Everytime I try the script doesnt work anymore.
cant really find currentSongIndex or songList.length in that code, I tried to change the numbers of currentindex and clip index, doesnt really help.

Also Auto Play in the SoundComponent only seems to work for what is inside the slots and doesn’t affect any clips in the clip-player.js

Hello @asnras,

The currentSongIndex and songList.lenght are simply suggestions for variables you may create. Also, Auto Play in SoundComponent by design only plays the first clip in clip-player.js.

If you’re new to coding and JavaScript, I recommend you take a look at this overview/course. You can get up to speed and code most things you’d like by completing it.

Taking a look at this tutorial one of our team members made may also help!