Advertisement

Corona SDK: Build an Audio Recording App

by

In this tutorial, you’ll learn how to create an audio recording app using the smartphone's microphone. Read on!


Step 1: Application Overview

Using pre-made graphics we will code a recording application with Lua and the Corona SDK.

The user will be able to record, play and replay the selected audio.


Step 2: Target Device

The first thing we have to do is select the platform we want to run our app within. That way we'll be able to choose the size for the images we will use.

The iOS platform has these characteristics:

  • iPad 1/2: 1024x768px, 132 ppi
  • iPad 3: 2048x1536, 264 ppi
  • iPhone/iPod Touch: 320x480px, 163 ppi
  • iPhone 4/iPod Touch: 960x640px, 326 ppi
  • iPhone 5/iPod Touch: 1136x640, 326 ppi

Because Android is an open platform, there are many different devices and resolutions. A few of the more common screen characteristics are:

  • Asus Nexus 7 Tablet: 800x1280px, 216 ppi
  • Motorola Droid X: 854x480px, 228 ppi
  • Samsung Galaxy SIII: 720x1280px, 306 ppi

In this tutorial we'll be focusing on the iOS platform with the graphic design, specifically developing for distribution to an iPhone/iPod touch, but the code presented here should apply to Android development with the Corona SDK as well.


Step 3: Interface

A simple and friendly interface will be used with a background and multiple buttons.

The interface graphic resources necessary for this tutorial can be found in the attached download.


Step 4: Export Graphics

Depending on the device you have selected, you may need to export the graphics in the recommended ppi, you can do that in your favorite image editor.

I used the Adjust Size... function in the Preview app on Mac OS X.

Remember to give the images a descriptive name and to save them in your project folder.


Step 5: App Configuration

An external file will be used to make the application go fullscreen across devices, the config.lua file. This file shows the original screen size and the method used to scale that content in case the app is run in a different screen resolution.

application =
{
    content =
    {
        width = 320,
        height = 480,
        scale = "letterbox"
    },
}

Step 6: Main.lua

Let's write the application!

Open your prefered Lua editor (any Text Editor will work, but you won't have syntax highlighting) and prepare to write your awesome app. Remember to save the file as main.lua in your project folder.


Step 7: Code Structure

We'll structure our code as if it were a Class. If you know ActionScript or Java, you should find the structure familiar.

Necessary Classes

Variables and Constants

Declare Functions

    contructor (Main function)
	
    class methods (other functions)

call Main function

Step 8: Hide Status Bar

display.setStatusBar(display.HiddenStatusBar)

This code hides the status bar. The status bar is the bar on top of the device screen that shows the time, signal, and other indicators.


Step 9: Background

This image will be used as the background for the application interface, the next line of code stores it.

-- Graphics

-- [Background]

local bg = display.newImage('bg.png')


Step 10: Pointer

These are the buttons that will start, play and stop the recording.

-- [Buttons]

local stopBtn
local recBtn
local play
local playAgain

Step 11: Variables

These are the variables we'll use, read the comments in the code to know more about them. Some of their names are self explanatory, so there will be no comment present.

-- Variables

local recording --the recording object
local recPath
local recorded --recorded audio

Step 12: Declare Functions

Declare all functions as local at the start.

-- Functions

local Main = {}
local addListeners = {}
local startRec = {}
local stopRec = {}
local soundComplete = {}
local replay = {}

Step 13: Constructor

Next, we'll create the function that will initialize all the game logic. We add the interface in this part.

-- Main Function

function Main()
	stopBtn = display.newImage('stopBtn.png', 170, 380)
	recBtn = display.newImage('recBtn.png', 70, 380)
	play = display.newImage('play.png', 116, 369)
	play.isVisible = false
	playAgain = display.newImage('playAgain.png', 130, 450)
	playAgain.isVisible = false
	
	addListeners()
end

Step 14: Add Listeners

This function adds the necessary listeners to the buttons for the app to work.

function addListeners()
	recBtn:addEventListener('tap', startRec)
	playAgain:addEventListener('tap', replay)
end

Step 15: Start Recording

This is the recording part.

First, we declare a path to save the corresponding audio file (usually this is the Documents directory) and set the recording object. Then we initialize it and hide the rec button.

function startRec(e)
	stopBtn:addEventListener('tap', stopRec)
	recPath = system.pathForFile('myRecording.aif', system.DocumentsDirectory)
	recording = media.newRecording(recPath)
	recording:startTuner()
	recording:startRecording()
	recBtn.isVisible = false
	
	transition.to(stopBtn, {time = 200, x = display.contentWidth * 0.5})
end

Step 16: Stop Recording

To stop the recording we simply call the stopRecording method. We also hide the buttons and display a playing message.

function stopRec(e)
	stopBtn:removeEventListener('tap', stopRec)
	recording:stopRecording()
	recording:stopTuner()
	
	stopBtn.isVisible = false
	play.isVisible = true

Step 17: Play Recorded File

The next lines of code play the audio once the recording has stopped. A function is called when finished, read more about it in the next step.

	-- Play recorded file
	
	recorded = audio.loadStream('myRecording.aif', system.DocumentsDirectory)
	audio.play(recorded, {onComplete = soundComplete})
end

Step 18: Sound Complete

This function returns the buttons to its original state. You can also see a commented line that will clear the audio from memory in case we no longer need it.

function soundComplete()
	--audio.dispose(recorded)
	
	recBtn.isVisible = true
	stopBtn.isVisible = true
	stopBtn.x = 217
	play.isVisible = false
	playAgain.isVisible = true
end

Step 19: Replay

A replay button will appear when the first recording is finished. The following lines handle the replay.

function replay()
	recBtn.isVisible = false
	stopBtn.isVisible = false
	audio.play(recorded, {onComplete = soundComplete})
end

Step 20: Call the Main Function

In order to initially start the game, the Main function needs to be called. With the above code in place, we'll do that here:

Main()

Step 21: Loading Screen

The Default.png file is an image that will be displayed right when you start the application while the iOS loads the basic data to show the Main Screen. Add this image to your project source folder, it will be automatically added by the Corona compliler.


Step 22: Icon

Using the graphics you created before, you can now create a nice and good looking icon. The icon size for the non-retina iPhone icon is 57x57px, but the retina version is 114x114px and the iTunes store requires a 512x512px version. I suggest creating the 512x512 version first and then scaling down for the other sizes.

It doesn't need to have the rounded corners or the transparent glare, iTunes and the iPhone will do that for you.


Step 23: Testing in Simulator

It's time to do the final test. Open the Corona Simulator, browse to your project folder, and then click open. If everything works as expected, you are ready for the final step!


Step 24: Build

In the Corona Simulator, go to File > Build and select your target device. Fill the required data and click build. Wait a few seconds and your app will be ready for device testing and/or submission for distribution!


Conclusion

Experiment with the final result and try to make your custom version of the app!

I hope you liked this tutorial series and find it helpful. Thank you for reading!

Advertisement
Related Posts