Problems playing sounds with Android MediaPlayer
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 sou开发者_Python百科nd:
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?
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();
}
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.
精彩评论