UDK Music Player

Yesterday I was playing a cool little Flash game that had a cool little feature: an in-game music player (much like the simple MP3 players we’re all used to) sitting in the bottom corner. That got me thinking…could I do the same thing in UDK?

It turns out, yes. The easiest place to put such a music system would be in your custom game class. These variables handle all the data necessary for a basic music player:

var int currentTrack, numTracks;
var AudioComponent musicTracks[5];

currentTrack would store the array index of the track currently playing. numTracks stores the total tracks available (for now this is hard-coded in the default properties). musicTracks keeps references to the actual SoundCues for all available tracks. Like numTracks, the size of the array must also be hardcoded. If you’re against hard-coding stuff you can look into dynamic arrays, but I find this method much easier to work with.

Then you can add the following functions:

function StartMusic()

function SwitchTrack(int newTrack)
currentTrack = newTrack;

function AdvanceTrack(int diff)
local int newVal;
newVal = currentTrack + (diff % numTracks);
if(newVal < 0)
newVal = numTracks + newVal;
if(newVal >= numTracks)
newVal = newVal % numTracks;

StartMusic() just begins playing the current track with a fade-in. SwitchTrack() handles switching to a new index, and sets the current index to the new one. AdvanceTrack() does the same thing, except it works with differences between indices. For example, you can call AdvanceTrack(1) to play the next track, or AdvanceTrack(-1) to play the previous track. You also don’t have to worry about null indices since AdvanceTrack handles adjusting for rollover; AdvanceTrack(-1) from the first song in the list will begin playing the last song, for example.

Lastly we have the default properties:

Begin Object Name=Music01Comp
SoundCue=SoundCue'[ PATH TO YOUR SOUND CUE ]'
End Object

You can copy the Object template for all tracks you want to add, just change the “Name” each time and store it in a different index of musicTracks.

Now the last thing to do is actually make the music player! This is a task for ScaleForm and not really in the scope of this post (plus I’m still working on it 😛 ), so I’ll leave it up to you. A few hints though:

  • For a “Previous” button, call AdvanceTrack(-1).
  • For a “Next” button, call AdvanceTrack(1).
  • For a “Stop” button, you will likely need to make a special function. Note that at this point it doesn’t look like UnrealScript supports pausing/resuming a music track.
  • For a “Track Select” table, grab the selected index of the table (same song order as you have stored in musicTracks) and then call SwitchTrack(index).

2 thoughts on “UDK Music Player

  1. When using % for negative numbers just do


    This will let remove the IF from the code

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s