public static MediaPlayer mp=null;
public static void playGeneric(int name, final ImageButton button,final ImageButton pervious,Context context) {
button.setEnabled(
As android docs suggest that if mp is if has not been initialized at that time java.lang.IllegalStateException will be thrown so you have to initilize first or you have to write
check out the docs http://developer.android.com/reference/android/media/MediaPlayer.html#isPlaying()
try like this
mp=MediaPlayer.create(context, name);
try {
if (mp.isPlaying()) {
mp.stop();
mp.release();
mp=MediaPlayer.create(context, name);
}
mp.start();
} catch (Exception e) {
}
Try changing mp.release()
into reset()
. that could help you.
use runOnUiThread for mediaRecorder prepare.
private boolean prepareMediaRecorder() {
mediaRecorder = new MediaRecorder();
runOnUiThread(new Runnable() {
@Override
public void run() {
mediaRecorder.reset();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setOutputFile(filePath);
try {
mediaRecorder.prepare();
} catch (IOException e) {
mediaRecorder = null;
return;
}
mediaRecorder.start();
recording = true;
}
});
return true;
}