3
votes

I'm having some weird problems playing sounds with the Android MediaPlayer.

In my View constructor, I'm doing this:

    clickSound = MediaPlayer.create(context, R.raw.ggclick);
    rightSound = MediaPlayer.create(context, R.raw.right);
    wrongSound = MediaPlayer.create(context, R.raw.wrong);

and then when I want to play the sound:

if(prefs.getBoolean("playClick", true))
    clickSound.start();

The files are all very short WAV files, and this used to work. I upgraded my phone to 2.2 (FRG83G) recently, and I think that's when the clickSound stopped working. The other two still work.

I tried switching the click sound to ogg, and then all three worked. Just for consistency, I switched the other two to ogg, and again the click sound stopped working, but the other two played successfully.

Does anyone know what's going on here?

2
Do you get any error messages? It might be that the clicksound is just too short to be played in the 2.2 mediaplayer in the format you have it.Eric Nordvik

2 Answers

3
votes

here is my fonction to play sounds from a raw resource:

protected MediaPlayer _mediaPlayer;  

public void playFromResource(int resId)     
     {
     if (_mediaPlayer != null)
         {
         _mediaPlayer.reset();
         }
     _mediaPlayer = MediaPlayer.create(this, resId);
     _mediaPlayer.start();
     } 
1
votes

If you are just firing very short samples (not songs or long tracks), and don't need to manipulate them on the go, you should use SoundPool instead.