Sunday, October 20, 2013

A Complete Android Game Audio Solution

While audible sound effects and music won't necessarily make any video game more fun, they definitely do make it more interesting and more immersive. Before I added any sounds to my new Android game, I didn't really notice they were missing because I was so focused on gameplay elements. But after adding them, whenever I muted my device (or disabled the audio in code for performance troubleshooting) it was obvious... something was missing.

Image By: Siddartha Thota


If you want to learn how to code audio for your Android app or game yourself, the Android developer site's Managing Audio Playback and Media Playback pages are a good place to start. There are also some good sites/blogs out there that mention how to play a single sound effect or play a single music file, but I had trouble finding a complete solution that handled sound effects AND music, so I created an audio class that allows you to easily play music and sound effects from any activity. I hope this will help others wanting to get started with audio in their Android apps/games.

My GameAudio.java file and its code are licensed under the Creative Commons Attribution License, meaning you may freely use it and adapt it to your needs as long as you credit me as the original author in a header comment. If you wouldn't mind posting a comment here telling me how you're using it, that would also be really cool and be a good incentive for me to share additional code in the future :)

Download GameAudio.java as a Zip File

View Source of GameAudio.java in a Pop-Up Window

Below is an Activity code snippet showing how to use the GameAudio class. Note the placement of the initialize and releaseAll calls in onResume and onPause, respectively. The playMusic and playSound methods can be called anytime after intialize and before releaseAll.
...
    @Override
    protected void onResume() {
        super.onResume();
        GameAudio.initialize(this, R.array.MenuSounds);
        GameAudio.playMusic(R.raw.music_title_screen, true, 1.0f);
    }

    @Override
    protected void onPause() {
        super.onPause();
        GameAudio.releaseAll();
    }
 
    public void onClick(View v) {
        GameAudio.playSound(R.raw.sound_button_press);
...
One final note, you may have noticed the initialize method takes an array resource input parameter -- R.array.MenuSounds in the snippet above. This is done to allow the GameAudio class to create a mapping between sound effect resource identifiers like R.raw.sound_button_press and the handle for the loaded sound created by the SoundPool.load function. If you only want to play music, you can pass null for that array resource identifier. Otherwise, create an entry like the one below in your res/values/arrays.xml file.

    
    
        @raw/sound_button_press
        ...
    
    ...

So that's it, the complete audio solution I'm using in my upcoming Android game. I hope this helps someone else just like the community's awesome docs, blogs, and websites have helped me.

1 comment: