[android-developers] Re: sound player
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+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home