Book Image

Lua Game Development Cookbook

By : Mario Kasuba, Mário Kašuba
Book Image

Lua Game Development Cookbook

By: Mario Kasuba, Mário Kašuba

Overview of this book

Table of Contents (16 chapters)
Lua Game Development Cookbook
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

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:

  1. 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 the nil value:

    local fileName = 'sound_file.MP3'
    local musicHandle = SDL.Mix_LoadMUS(fileName)
  2. From this moment, you can use various functions to control music. The first one, SDL.Mix_PlayMusic, is...