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.

Friday, October 18, 2013

Changing It Up

Image By: GollyGforce
The season's are changing, and it's been a long time since I switched anything up here on the GDJ blog, so I figure it's time for something drastic. OK, those who know me realize it's more of a cautious type of drastic. But anyway, I'm mixing some things up on the look and feel. The BIGGEST change, of course, is that I'm finally switching from white-text-on-a-black-background to the way most people, or at least non-coders, see text: white on black. I originally chose the black background with white text for a couple reasons: 1) I liked it and thought it looked cool, and 2) I thought it made screenshots really pop. But mostly I just thought it looked really cool.

Why change? Because we must. And because it's Fall. And because it's fun to mix things up once in a while. I'm still tweaking a little bit, so you may notice additional changes over the next couple weeks, but I think the white text on black background will be here for a while. Hope you like it, but if not let me know!

Thanks for holding tight while the dust settles...

Saturday, October 12, 2013

Rave Reviews Are IN!!!

Image By: Sarah Reid


The feedback is in, and so far people are LOVING my new Android game!
"Addicting!" - Anonymous Family Member
"I like this game. It's really fun!" - Anonymous Friend
"He can't stop talking about your game, it's all he ever talks about!" - Anonymous Mother

OK, OK, in all fairness these reviews aren't exactly new-ish. And I do mention somewhat tongue-in-cheek that these are anonymous sources because I haven't requested permission to use their names. But these quotes DID happen, and all I can say is that there is quite an amazing rush when someone else picks up something you've poured countless hours into and says "hey, this is pretty fun."

Since the time of those feedbacks I've added sounds, music, and a number of visual (particle) effects to raise the bar. So if it was already fun then... well, I hope it's only getting better!

Wednesday, October 9, 2013

What's YOUR backup strategy?

Recently, I was working on my afore-mentioned forthcoming Android game. I currently use Subversion, coupled with TortoiseSVN, locally on my development laptop for change management. So if I get to a solid point where I'm happy with my changes, I can commit them to my local repository. Then if I later try something new and don't like my latest work I can just revert back to the last "golden" build.

But there is a certain danger in this development flow. As I mentioned, I'm using Subversion locally on my development laptop. And every now and then, I get a sneaking suspicion that my laptop is just waiting to die on me right before I commit a large chunk of code with the most amazing changes I've ever made! So what is a poor indie game developer to do?

In the past, collaborating with a team on a small PC game, my team used a commercial hosting plan. So our code was committed to a remote repository and that worked as our off-site backup. But for my small Android game, I didn't really want to pay for hosting OR use an open-source repository. So I've actually been using the much lower-tech method of zipping up my code folder and storing occasional backups on DropBox or my Google Drive. That gets the job done; I just worry I may not be doing that frequently enough, and like I said the laptop could choose to give up on any given day. One thing I've learned over the years is "Save early, Save often." and to not trust hardware, because it will let you down when you least expect it.

So what's YOUR backup strategy? Leave a comment to let me know! But excuse me... I need to go, uh... make another backup... :)