Splash Screen Demo With Sound

I did a little experiment to see if I could add sound to my splash screen demo. I never had done anything with sound in Android but had wanted to try it out for quite some time. It turns out that it was very easy to do.

The demo app looks exactly like the previous Splash Screen demo. The only difference is that it nows plays sounds.  It plays two distinct sounds. The first is for the splash image. The second is for the main screen. The second sound plays when the main activity starts and at each time you reorient your Android device.

Here’s what I did:

1. I moved two mp3 and wav files into the res/raw folder.

2. In my activity’s onCreate method, I added code to create a new MediaPlayer objects. Then I started it playing. The code looks like this:

mp1 = new MediaPlayer (this, R.raw.laser_01);
mp1.start ();

3. In my activity’s onDestroy method, I added code to stop the media player and release any resources it had.

if (mp1 != null) {
   if (mp1.isPlaying ()) mp1.stop ();
   mp1.release ();
   mp1 = null;

If you are interested in the source code for the demo, here is a link to a download page for it: download Splash with Sounds.

In researching this, I came up with some questions that I want to follow up on.

  • When should you use SoundPool and when should you use MediaPlayer?
    From what I can tell so far, it seems like SoundPool is best for adding sound effects for apps. The MediaPlayer is better for longer sounds.
  • What’s the best way to maintain the state of a MediaPlayer?
    I had some trouble when I created the media player in the onCreate method and starting it onResume. I was getting an exception about the media player being in an illegal state. So I don’t yet understand the MediaPlayer methods fully.
  • How long is too long for a sound file being played in the main (UI) thread?
    The example I built plays the sound on the main thread. If the duration of the sound were longer, it would be good to move it into a background task.


Media Playback note – general information on the Android Developers website about the MediaPlayer

Question about sound on Stack Overflow

Android Sounds Tutorial by Lars Vogel – Example of a SoundPool. This seems like the way to go to have a set of sounds you use over and over again.

About Bill Lahti

Bill Lahti is an indie game developer. He is currently working on Lifeforms, a two-player game based on Conway’s Game of Life. Other iOS and Android games: Double Star II, Wing Leader, Gomoku League.
This entry was posted in Android and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.