Playing background music
Music playback works similarly to the sound sample playback with one exception that there's only one music channel and it's controlled separately from the other mixing channels.
What's more, music playback can be moved into a specific position, so it can be used as a simple music player if that's your intention.
Getting ready
The music channel is allocated automatically, so there's no need to allocate more mixing channels to play music.
How to do it…
The first step in playing music is:
To load a sound file into memory with the
SDL.Mix_LoadMUS
function. In this case, you can use a different set of file formats, such as WAV, MOD, MIDI, OGG, MP3, or even FLAC. This function returns a handle upon success similar to sound samples. If there's a problem loading sound files, it returns thenil
value:local fileName = 'sound_file.MP3' local musicHandle = SDL.Mix_LoadMUS(fileName)
From this moment, you can use various functions to control music. The first one,
SDL.Mix_PlayMusic
, is...