Joy of Android: Playing Sound

I do really like Android, unsuspectingly so. It was my New Year’s resolution to finally write an app, and I got this assignment at work to create… the quick business card app. Available in the marketplace. 10 installs in 1 day! Already an upgrade!

OK so I decided to port one of our iPhone apps- French Bingo- over to ‘droid. I immediately ran into a problem. How to play a sound. It wouldn’t play. I copied code from examples, it wouldn’t play. As in most the debugging helped me learn more about the environment, but to cut to the chase, here was the solution:

Enable the emulator to play sound. (d’oh!)

To do this:
1) Launch Eclipse (for Mac), select Run->Run Configs.
2) Select “Android Application” (left pane)
3) Select the “Target” tab in right pane.
4) Scroll down to the hidden field, “Additional Emulator Command Line Options”
5) add “-useaudio”

(Geesh). Yes, my biggest gripe so far with development is that it’s Eclipse environment on a Mac. Most documentation is for Eclipse on a PC, and things like enabling audio are too simple to mention in most blog posts or examples. So here! The internet has a blog post about it.

Here is the code for a simple example of playing a sound. My pet peeve is folks not including library/class calls, so here they are in entirity!

package com.banane.bingueau;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class BingueauActivity extends Activity {

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
         
        }
        public void playWord(View v){
                try{
                        MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.salon);
                        if (mp != null) {
                                mp.start();
                        }
                        mp.setOnCompletionListener(mCompletionListener);
                } catch (Exception e){
                        //Log.d("error",e.getMessage());
                }
        }

       
        private MediaPlayer.OnCompletionListener mCompletionListener = new MediaPlayer.OnCompletionListener() {
            public void onCompletion(MediaPlayer mp) {
                mp.release();
            }
        };
}

My XML for the layout has a button, “word1″ with an “onClick” attribute. That’s how I’m listening for the event:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:stretchColumns="1">
    <TableRow>
        <Button
           android:id="@+id/word1"
           android:width="100dp"
           android:text="@string/word1"
           android:padding="3dip"
           android:onClick="playWord"
           />
  </tablerow>
</tablelayout>
</xml>

Any comments/advice/thoughts are welcome of course.

  • Pam Selle

    Nice! The app I just wrote plays a sound on a loop - I should post my code!

  • Yeah that'd be awesome!

  • I'm surprised at how fast it's been to get stuff done- not sure if this is because mobile dev is so similar architecturally to iPhone, or because my Java skills aren't as rusty as I thought? Not sure. Or, it's just a more welcoming language/environment/architecture.

  • rubygeek

    Nice, I've dabbled a bit in android andĀ Its kinda fun :)

blog comments powered by Disqus