Adding music while keeping app size small

Ok, you’ve made progress on your game and things are coming together. Your gameplay is polished and actually pretty fun. Your artwork is looking great. It’s time to add that cherry on top that completes your game experience…music!

There’s only one problem, you’d like to have a bunch of songs in your soundtrack but high quality music files take up a lot of space. A 1 minute track could easily take up 5MB depending on what audio format you use. In the mobile world, space is precious, especially on Android where your apk size for the Play Store is limited to 50MB (unless you want to deal with pesky expansion files).

I faced this problem recently when adding music to my game. At first, I wanted to keep things simple so I chose the .wav format for cross-platform audio compatibility. But I soon learned that all my audio files combined were adding a hefty 20-25MB to my game! I tried reducing the sample rate, which made the files a little smaller but the audio quality suffered greatly.

I was not content so I did some googling and found a combination of tricks to trim some of the fat in my app.

Going back to that page on Corona audio formats, I noticed that Android supports .ogg and iOS supports .aac. These file formats are compressed but offer great audio quality, and best of all, they don’t come with weird royalty restrictions like .mp3 does. I found this neat website that will convert your formats for you for free, and it even gives the option to change certain settings and trim the length of the song. You can check it out here (Note: unfortunately, I recommend using an ad blocker on this site as they have some intrusive ads). The settings that I recommend to keep the best sound are

Bitrate: 128 kbps
Sampling Rate: 44100 Hz
Audio Channels: Mono

Now, you may be saying to yourself, “I have double the amount of files, how does this help me reduce the app size?”. There are still a couple more pieces we need to make this work.

First, let’s take a look at a simplified audio manager that automatically selects the file format based on what platform we are on:

Lastly, let’s look at where the magic happens. Corona has this awesome feature that lets you exclude files from your build, and it even supports excluding certain files based on the platform you are targeting! Open up your build.settings file and drop this into the settings table:

And there you have it! Compressed audio, dynamic format selection, and reduced app size. Your game is now one step closer to being production ready. Happy coding!

Facebooktwittergoogle_plusredditFacebooktwittergoogle_plusreddit

Leave a Reply

Your email address will not be published. Required fields are marked *