7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial
  1. Code
  2. Corona SDK

Corona SDK: Creating a Music Player - Application Setup

Scroll to top
Read Time: 5 mins

In this two part tutorial, we'll use the Corona SDK to build an attractive Music Player. Read on!

Step 1: Application Overview

Figure 1

Using premade graphics we will code an entertaining game using Lua and the Corona SDK

The user will be able to play and control predefined audio files as well as see information about them.

Step 2: Target Device

Corona SDK - Audio Player - Figure 2Corona SDK - Audio Player - Figure 2Corona SDK - Audio Player - Figure 2

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

The iOS platform has these characteristics:

  • iPad: 1024x768px, 132 ppi
  • iPhone/iPod Touch: 320x480px, 163 ppi
  • iPhone 4: 960x640px, 326 ppi

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

  • Google Nexus One: 480x800px, 254 ppi
  • Motorola Droid X: 854x480px, 228 ppi
  • HTC Evo: 480x800px, 217 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

Corona SDK - Audio Player - Figure 3

A dark, nice looking interface will be displayed, this involves multiple shapes, buttons, bitmaps and more.

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

Step 4: Export Graphics

Corona SDK - Audio Player - Figure 4Corona SDK - Audio Player - Figure 4Corona SDK - Audio Player - Figure 4

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 save them in your project folder.

Step 5: Songs

Figure 5

Choose a few songs from your music library and place them in your project folder. We will learn how to play them in the next steps.

Step 6: 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.

Step 7: Main.lua

Let's write the application!

Open your preferred 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 8: 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.

Step 9: Hide Status Bar

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 10: Background

Figure 6

A simple dark brown image is added as the background for the application interface.

Step 11: Information Bar

Figure 7

The information bar will contain the album art, song name, and artist. It will be placed in the center of the stage.

Step 12: Album Art

Figure 8

A variable is created to store the album art image, this image needs to be in the project folder. More about this in the last step.

Step 13: Information Text

Figure 9

The song name and artist are stored by the following variables:

Step 14: Buttons Bar

Figure 10

This bar displays the controls buttons as well as the progress bar and the song duration.

Step 15: Songs Data

XML will probably be the best way to handle the song information, but unfortunatelly at this point there's not a native XML API in Corona. While you can still use some Lua libraries like LuaXML, it will be faster to create a Table for short content as shown in the next lines:

As you can see in the code above, Tables are used to store the songs information, which is later used by the audio.loadStream() method to create an audio streming object.

Step 16: Code Review

Here is the full code written in this tutorial alongside with comments to help you identify each part:

Next Time...

In this part of the series you've learned the interface and the basic setup of the application. Stay tuned for part two where we will handle the logic of the application, buttons behavior and more. See you next time!

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.