I am trying to play a sound file on the click of a button. The sound is just 1 sec long. It plays well the first few times I click the button, but after a while it gives a <
If still your issue not solved then try this as it help me.
public void playSound(int resources){
try{
boolean mStartPlaying = true;
MediaPlayer mPlayer=null;
if (mStartPlaying==true){
mPlayer = new MediaPlayer();
Uri uri = Uri.parse("android.resource://YOUR_PACKAGENAME/" + resources);
mPlayer.setDataSource(getApplicationContext(),uri);
mPlayer.prepare();
mPlayer.start();
}
else{
mPlayer.release();
mPlayer = null;
}
mStartPlaying = !mStartPlaying;
}
catch (IOException e){
Log.e(LOG_TAG, "prepare() failed");
}
// MediaPlayer mp = MediaPlayer.create(SpyMainActivity.this, resources);
// mp.start();
}
Enjoy. :)