Game Development Reference
In-Depth Information
Creating Sound
While we've covered the visual and tactile parts of game play, to complete the experience, we need
to have some sound. This sound can be simple, such as the sound when an element on the screen
is touched, or it can be more complex, such as the sound of explosions or pulsating music. We
might also want to include a video for advertising purposes, as a replacement for a help feature, or
for helping transition the game's story from one part to another. We'll look at creating video in the
next section.
Let's Make Some Noise
Corona SDK has two sets of namespaces that can be used for audio. Initially it used the media
namespace, but it's now pushing toward making media redundant, and wants developers to use the
audio namespace with OpenAL (for maximum Android compatibility). OpenAL stands for Open Audio
Library, a cross-platform audio library. The API style and conventions resemble those of OpenGL.
OpenAL also provides multiple simultaneous channels for playing sounds, so you can really layer
sounds. For example, you could simulate the sound of sitting at a café outside on a lovely day—
hearing a person singing and playing a guitar, the chatter of other people talking, traffic, and so on,
all at the same time.
To start with the basics, let's say we want to play a click sound, and the audio file we have is click.
wav . We can simply use the playSound function from the media namespace:
media.playSound ( "click.wav" )
We can also use the playEventSound function to play a short sound (between 1 and 3 seconds).
This requires that we load the sound prior to playing it. This function does not take a file name, but
instead takes a handle to the loaded sound data.
local snd = media.newEventSound ( "click.wav" )
media.playEventSound ( snd )
The playSound function can also be provided additional parameters that allow for the sound to be
looped or a function be called when the sound stops playing.
This can be used to create a simple MP3 jukebox, like so:
local listMP3 = {
"track1.mp3",
"track2.mp3",
"track3.mp3",
"track4.mp3",
"track5.mp3",
"track6.mp3"
}
local currentTrack = 0
--
 
Search WWH ::




Custom Search