Corona – Audio

Corona makes it easier than anything to get sound into a project. The documentation says:

Sound Formats

  • All platforms support 16-bit uncompressed .wav files.
  • iOS and Mac Simulator support .mp3, .caf, and .aac.
  • Windows Simulator supports .mp3 along with .wav.
  • As of build 269, Android now supports .mp3 and .ogg formats.

So it looks like you can use wav, mp3, caf and aac on IOS. Since I’m focusing on the IOS platform I’ll stick with one of these formats. You can use garage band to generate AAC and MP3 files.

To get a sound to play you need to load the file creating a reference to the audio object in the process. Then tell that audio object to play.

Try it for yourself. Create a new Corona project, follow these steps:

  1. Create a folder to store your project.
  2. Create a new text file and save it into your folder with the name main.lua.
  3. Find a suitable sound file and copy it to the folder.

Add the following Lua code to your main.lua file. The file I for the example was named beep.mp3. Be sure to replace this name with the name of your sound file.

local mySound = audio.loadSound( "beep.mp3" )
audio.play( mySound )

Pretty simple huh? The first line loads your sound. The next line tells the sound to play.

Resources:

  1. cfxr – A sound tool for the Mac

2 thoughts on “Corona – Audio”

  1. audio.loadSound is the perfect solution for short sound effects in Corona, such as a button press or an enemy exploding or points being added to your score.

    For longer sounds ( > 15s ) , audio.loadStream will allow your larger audio files to play without having to first load the entire file into your devices RAM. This lets your audio files to play much smoother while leaving more memory free for other processes.

    I did encounter a rather frustrating problem with audio.loadStream recently that I’d like to share in hopes i can save others the same frustration. The following is for more advanced users who have lots of sounds and background music in their app.

    While playing some voice-over clips in my app ( ~1-2min each ), I was experiencing some stuttering/skipping of the audio with occasional dropouts. I was certain that it wasn’t a RAM issue and confirmed my files were being caught by the garbage collector and all references were nil’d after audio.dispose( myHandle ), but still had this strange problem that seemed to happen at random.

    I ultimately discovered this ended up being due to an iOS5 bug in OpenAL. The current ‘Free Trial’ build of Corona ( 591 ) has not resolved this issue, but if you are a subscriber, it was fixed in build 645. Be sure to subscribe and grab the latest build for the best audio support.

    There are also some additional ‘hidden API’ params you can set to have your long audio files play even smoother. Add them to your audio.loadStream to increase the buffer size like this:

    local myBigAudioFile = audio.loadStream( audioFile.m4a, {bufferSize=8192, maxQueueBuffers=12, startupBuffers=4, buffersQueuedPerUpdate=2})

    Happy Building!

Comments are closed.