How do I make skippable music tracks?
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.
asked Sep 28 '17 at 12:36 PM in Blueprint Scripting
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.
2) 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 (https://github.com/RLangridge/AnswerProject).
answered Sep 29 '17 at 12:39 AM
Follow this question
Once you sign in you will be able to subscribe for any updates here