Saturday, April 19, 2014

Programming on PSP - Lua Dev Tutorial#3 - Using Sounds


Okay now that we know how to use images, the steps are basically the same. First you load your sound file, and then you play it whenever it’s needed. There are functions that allow you to loop the sound file (to keep it playing continuously) and pause the sound file.


Loading a sound file

LuaDev only loads MP3, AT3, BGM and WAV files. So keep that in mind. To load a sound, you must set a variable to the sound file. For example:

bgmusic = sound.load("Sounds/music.wav")

The “sound.load” operation locates the sound file called “music.wav” in the “Sounds” folder and loads it into our program.


Playing a sound file.

Now that we’ve loaded our sound file, we can play it whenever we need to. To do this we use this function:

sound.play(bgmusic)
 
The “sound.play” operation plays the sound that we loaded earlier, which was defined as a variable, “bgmusic”

Pausing a sound file

To pause a sound file, we use this function:

sound.pause(bgmusic,-1)

The number “-1” indicates that the pause/play function is automatic. If not paused it pauses, if paused it resumes.
You can use “0” to resume the sound if paused and “1” to pause the sound if playing. (make sure you use a comma after indicating the variable)


Stopping a sound file

To stop a sound file we call this function.

sound.stop(bgmusic)

This is pretty self-explanatory. I don’t think I need to get into detail with that.


To keep playing a sound file constantly – a loop.

To use this we call this function “sound.loop()”
For example:

sound.loop(bgmusic)

This will keep playing the sound file continuously until the loop is stopped. To stop the loop we use this function.

sound.loop()

Or you can free the sound file, using this function – This also frees more memory, it is recommended that you used this function.

sound.free(bgmusic)
 
That's all for now folks
 
Written By Joel16 & Edited by Ahmed Aziz

Widgets

 

Copyright @ 2014 CPP Fuzz.