How do I make skippable music tracks?

Hi folks,

I’m trying to think of a way that I can play audio music tracks in a skippable way. For example I want to have 5 tracks that play from 1 to 5 in order, but I’d like the player to be able to press a key to move to the next track with the audio fading out and also to be able to go back to the previous tracks by pressing another key.

I’ve played around in Blueprints but haven’t had much luck with stopping the tracks and going backwards on pkey press.

Any ideas would be appreciated.

Hi there. I had a bit of a play around with this in UE 4.17, testing with several tracks. Please note this probably isn’t the most elegant solution, however it seems to work as a basic premise. I created this based on the First person shooter blueprint template.

  1. I created an actor to place in the level which I named MusicPlayer (Totally original I know). This actor has a public array of SoundWave assets that can be added to. It also has two public floats: Fade In Time and Fade Out Time. These denote how long it takes to fade in and fade out between music tracks respectively. There is also a private integer (named Music Pointer) which I use to keep track of which soundwave asset should be playing. The actor has an Audio component which allows for the Soundwave assets to be played.

  1. The MusicPlayer actor has 3 main functions; Play Music, which is intended for when the music begins initially. It tells the Audio component to set a Soundwave to play and then tells the Audio component to play the Soundwave.

Go To Next Song fades out the song that is currently playing (The fade goes for the duration denoted in Fade Out Time). While the Audio component is performing the fade out, a delay is set for the duration of Fade Out Time. Once the delay is complete, the music pointer is incremented, another soundwave selected and the soundwave is then faded in (It fades in based on Fade In Time).

The Go To Previous Song function essentially does the same thing, just selecting the previous song as opposed to the next one.

The Set Music Pointer function is just ensuring that the music pointer doesn’t go outside an expected range so can always select a sound from the Music To Play array.

To use this blueprint, you can place it into the level you’re building and just add in soundwaves. To interact with this object, I use a Player Controller as a messenger between the MusicPlayer actor and a UI. If you want to use keys to go between songs, you can set up an action mapping in the Input section of the Project Settings (Edit > Project Settings… > Input). You could then use your player controller to implement those action mappings and have it talk to the MusicPlayer to tell it to go to the next song or go back to a previous song (In my project, I created two input action mappings: Next Song and Previous Song then implemented them in the player controller. They then call the respective functions of the MusicPlayer.)

One last note on the functions; using the begin play event, I set a reference to the MusicPlayer for the player controller to use. This allows for the MusicPlayer functions to be utilised.

If you would like to see my basic implementation, I’ve placed it up on github (GitHub - RLangridge/AnswerProject: This repo contains the code to questions that I attempt to answer on the Unreal Answerhub.).

Hi Hightype,

Thanks for your answer!

I’ll take a look and see if I can get it to work in my project.

I tried following your Github link but the link seems dead though.

I’ll let you know how I get on with your Blueprint.

Many thanks,

Paul

I’ve made the array, which is an actor array (is that correct) I can’t add any of my music to the array, and I can’t get to link to the the sound. It gives this error (array of actor references is not compatible with sound base reference).

Hi Paul,

Sorry that I hadn’t responded earlier. I think I may have grabbed a space in the link: hopefully this one should work: GitHub - RLangridge/AnswerProject: This repo contains the code to questions that I attempt to answer on the Unreal Answerhub. The array that you will want to make is an array of SoundWave objects. In my example above, the Music To Play array is an array of SoundWave objects. From there you should be able to add in music. Please let me know if you need more assistance or if my link still isn’t working.

Hi HighType,

Thanks for replying again. I’ve managed to download your link and I’ll hopefully be trying it later today.

Many thanks,

Paul

Hello,
Just a quick question… Can i then display on a Widget the name of the song that is playing? If so then how?