This Question is already asked but I am unable to find out my solution .
What i am doing is, MotionEvent.ACTION_DOWN
of OnTouchListener
I started recording audio and in MotionEvent.ACTION_UP
I stopped audio recording.
During long touch of that icon it is working fine but onclick
app is crashing.
Here is my
audio.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Toast.makeText(Chat.this, "You Have to hold the Button for audio recording.", Toast.LENGTH_LONG).show();
}
});
audio.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
MediaRecorderReady();
try {
mediaRecorder.prepare();
mediaRecorder.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else if (event.getAction() == MotionEvent.ACTION_UP) {
if (mediaRecorder != null) {
mediaRecorder.stop();
}
}
return true;
}
});
public void MediaRecorderReady() {
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
mediaRecorder.setOutputFile(AudioSavePathInDevice);
}
Here is my problem , when I am clicking on that button the app is crashing
Check this answer Android mediarecorder stop failed and Also Read the offical documentation about mediaRecorder and its states. https://developer.android.com/reference/android/media/MediaRecorder.html crash is caused because you stop the media player and media player not in recording state.. donot call stop directly when it is not in recording state.
try {
mRecorder.stop();
}
catch(RuntimeException e) {
}
finally {
mRecorder.release();
mRecorder = null;
}