We're going to learn how sound effects and music are implemented in Corona to get an idea of how it really works. To play an audio follow the steps:
Playing Audio
.Chapter 6 Resources
folder, copy the ring.wav
and song1.mp3
sound files into your project folder and create a new main.lua
file. You can download the project files that accompany this book from the Packt Publishing website.loadSound()
and loadStream()
:ringSound = audio.loadSound( "ring.wav" ) backgroundSound = audio.loadStream( "song1.mp3" )
backgroundSound
by setting it to channel 1, loop it infinitely, and fade in after 3 seconds:mySong = audio.play( backgroundSound, { channel=1, loops=-1, fadein=3000 } )
ringSound
and play it once:myRingSound = audio.play( ringSound )
For audio that is merely a short sound effect, we used audio.loadSound()
to prepare the sound. For audio that is large in size or long in duration, audio.loadStream()
is used.
The backgroundSound
file is set to channel 1, and fades in at 3 seconds when it starts playing. The loops = -1
statement means that the file loops infinitely from beginning to the end.
As you can see, loading and playing an audio is really simple. It only takes two lines of code to play a simple sound. Let's see if you can take it up a notch.
Use the ring.wav
file and load it through loadSound()
. Create a function that plays the audio. Have the sound play at an interval of 2 seconds, repeating five times.