Part 5 – First Steps – Audio

Introduction to Mobile Game Development

Written July, 2015, by Rachel J. Morris

Creative Commons License Introduction to Mobile Game Development by Rachel J. Morris is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Based on a work at

Return to the main page

With Gideros, we can play music and sound effects via the Sound class of objects. It’s as simple as pointing a new Sound object to a file’s path, and calling the play function.  There are also useful functions like setPaused, setVolume, setLooping, setPitch, and setPitch. You could essentially write your own mobile audio player! :)

As always, Gideros has great documentation on the functionality they provide. You can see the SoundChannel documentation here.

Let’s get started with how to use sounds and music!

Playing Sounds

Create a new project, and we’ll make a super simple app that plays a noise when it starts up. Make sure to move a sound file to your project folder on the hard drive. Right-click the project file and click Add Existing Files… to add our sound effect, and click Add New File… to add a main.lua source file.

Creating our Sound object is as simple as calling new

effect = "content/Jump11.wav" )

and then to play, we just call play


Run the program by clicking on the Start Gideros Player button and then Play.  That was pretty simple, right?

Playing Music

You can play a music file in the same way we played a sound effect, but you might want to do more with music, like pause, adjust volume, change speed, or even fade between two different pieces of music! In order to do more than simply play our sound files, we need to store it in a SoundChannel so we can make changes after the file is already playing.

To start a sound file and store it in a channel, we just make a new variable to store it in:

music = "content/SuburbanDinosaur_Moosader.mp3" )
local channel = music:play()

Now, we can modify the music in different ways.

Adjust the volume

You can adjust the volume of the SoundChannel at any point within your program.  Using the setVolume function, you pass in 0.0 for silent, 0.5 for half-volume, 1.0 for full-volume, and anywhere between these numbers for other volumes.

music = "content/SuburbanDinosaur_Moosader.mp3" )
local channel = music:play()
channel:setVolume( 0.5 )

Adjust the pitch

With the setPitch fuction, setting it to 1.0 will be normal speed. Above 1 will make the pitch higher, and lower than 1 will make the pitch lower.

music = "content/SuburbanDinosaur_Moosader.mp3" )
local channel = music:play()
channel:setPitch( 0.5 ) -- low
channel:setPitch( 1 ) -- normal
channel:setPitch( 1.5 ) -- high

Set the position within the song

If you wanted to rewind the song, or fast-forward to another position, you can use the setPosition function.  This function takes miliseconds, rather than a percentage like volume and pitch.  If you wanted to go one second into the song, you’d pass in 1000.

music = "content/SuburbanDinosaur_Moosader.mp3" )
local channel = music:play()
channel:setPosition( 5000 ) -- 5 seconds in

Set it up so that the song loops

If you want the sound effect or song to keep looping infinitely (or until you pause or stop it), you can use setLooping to true.

music = "content/SuburbanDinosaur_Moosader.mp3" )
local channel = music:play()
channel:setLooping( true )

Pause and resume the song

You can also use the setPaused function to pause and unpause the sound effect or song.

music = "content/SuburbanDinosaur_Moosader.mp3" )
local channel = music:play()
channel:setPaused( true ) -- Pause
channel:setPaused( false ) -- Unpause

Stopping the song

If you were to stop the song instead of pausing it, your channel is going to lose all its settings (volume, pitch, position), and if you load a new song into the channel it will have all its defaults set.

music = "content/SuburbanDinosaur_Moosader.mp3" )
local channel = music:play()
channel:setPosition( 5000 )

On the class GitHub page, there are two projects with sample code for audio:

Also check out the related Gideros documentation:

Print Friendly