I am attempting to play a sound using a MediaPlayer object, but I cannot seem to get it to work despite my best efforts. The sound simply refuses to play.
It’s a short sound, that is supposed to be played when the screen is touched, meaning it will have to be repeated many times without too much delay. Knowing this I followed the state diagram, http://developer.android.com/reference/android/media/MediaPlayer.html. I can’t seem to see what exactly is wrong with my sequencing of method calls.
MediaPlayer mp = MediaPlayer.create(this.getContext(), R.raw.select2);
try {
mp.prepare();
mp.start();
Log.e("debug","sound played");
}
catch(Exception e) {}
mp.stop();
You call
prepare()on the mediaplayer, but the create() call you use prepares the player automatically, this causes anIllegalStateExceptionwhen you try to callprepare()again, and you are sent to yourcatch()(you would have noticed this if you handled the exception in some way, i.e. printing the stack trace).