Game Development Reference
In-Depth Information
As an exercise, try to make a UI that can display the name of the current track being played, and
place some buttons on the screen for playing, pausing, and skipping to the next or previous track.
Using OpenAL Sound
The audio namespace includes the OpenAL functions, which might eventually replace the media.
playSound and media.playEventSound functions.
With OpenAL, you need to load the sound and then dispose of it once you are done. If you don't,
you can end up leaving sounds in memory and soon run out of memory.
local snd = audio.loadSound( "click.wav" )
local channel = audio.play( snd )
We have 32 channels on which we can play our sounds. Each time we use the function audio.play ,
the API looks for a free channel and starts to play the sound on that channel, and it returns the
channel number back to us. It is with this channel number that we can pause or stop the sound from
playing on that channel. We can also query the channel to determine the state of the channel, using
any of the following:
audio.isChannelActive
audio.isChannelPaused
audio.isChannelPlaying
Once we have finished playing the sounds, we are responsible for disposing of (freeing up the
memory used for) the sound using the audio.dispose function.
audio.dispose ( snd )
Setting the Volume
With OpenAL, we can set not only the master volume, but also the volume for each of the channels.
Similar to the volume in the media namespace, the level ranges from 0 (silent) to 1.0 (loudest).
function increaseVolume()
local currVol = audio.getVolume ( )
if currVol >= 1.0 then return end -- Already at the max volume
media.setVolume ( currVol+0.1 )
end
--
function decreaseVolume()
local currVol = media.getVolume ( )
if currVol <= 0 then return end -- Already at the min volume
media.setVolume ( currVol-0.1 )
end
Notice that we are using the same name for the function that we used previously, and we're
substituting the media functions with the audio functions to manipulate the volume. This way, if
the code is encapsulated within a generic function, we can use the functions increaseVolume and
 
Search WWH ::




Custom Search