Here's code that I am using repeatedly in several apps... hope this
helps!  :)

final class PlaySound {
        public static boolean playing=false;

    private static HashSet<MediaPlayer> mpSet = new
HashSet<MediaPlayer>();

    static void play(Context context, int resId) {
        playing=true;
        MediaPlayer mp = MediaPlayer.create(context, resId);
        mp.setOnCompletionListener(new
MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                mpSet.remove(mp);
                mp.stop();
                mp.release();
                playing=false;
            }
        });
        mpSet.add(mp);
        mp.start();
    }

    static void stop() {
        for (MediaPlayer mp : mpSet) {
            if (mp != null) {
                mp.stop();
                mp.release();
                playing=false;
            }
        }
        mpSet.clear();
    }
}

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to