I am currently trying to record audio on long tap and hold. The application works when I tap and hold, however, the app crashes when I click the button instead. Please help me figure out why this is happening.
p/s: Also, can someone tell me how to format log to display here? I have hard time with that
Code
public class MainActivity extends AppCompatActivity {
private static final String LOG_TAG = "Hello";
TextView recordDisplay;
Button recordButton;
private MediaRecorder mRecorder = null;
private String mFileName = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recordDisplay = (TextView) findViewById(R.id.dislayRecordView);
recordButton = (Button) findViewById(R.id.recordButton);
mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
mFileName += "/SKBeon_recording.3gp";
recordButton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if(motionEvent.getAction()==MotionEvent.ACTION_DOWN){
startRecording();
recordDisplay.setText("Recording Started!");
}else if (motionEvent.getAction() == MotionEvent.ACTION_UP){
stopRecording();
recordDisplay.setText("Recording Sopped!");
}
return false;
}
});
}
private void startRecording() {
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(mFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mRecorder.prepare();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
mRecorder.start();
}
private void stopRecording() {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
}
Xml main:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<TextView
android:id="@+id/dislayRecordView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/recordButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Record"
/>
</LinearLayout>
LogCat:
- 09-04 01:28:51.618 22535-22535/english_malay.my.apps.com.recordaudio E/MediaRecorder: stop failed: -1007
- 09-04 01:28:51.619 22535-22535/english_malay.my.apps.com.recordaudio E/InputEventReceiver: Exception dispatching input event.
- 09-04 01:28:51.619 22535-22535/english_malay.my.apps.com.recordaudio E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
- 09-04 01:28:51.621 22535-22535/english_malay.my.apps.com.recordaudio E/MessageQueue-JNI: java.lang.RuntimeException: stop failed.
* at android.media.MediaRecorder.stop(Native Method)
* at english_malay.my.apps.com.recordaudio.MainActivity.stopRecording(MainActivity.java:70)
* at english_malay.my.apps.com.recordaudio.MainActivity.access$100(MainActivity.java:18)
- 09-04 01:28:51.621 22535-22535/english_malay.my.apps.com.recordaudio D/AndroidRuntime: Shutting down VM
- 09-04 01:28:51.622 22535-22535/english_malay.my.apps.com.recordaudio E/AndroidRuntime: FATAL EXCEPTION: main
- Process: english_malay.my.apps.com.recordaudio, PID: 22535
java.lang.RuntimeException: stop failed.
* at android.media.MediaRecorder.stop(Native Method)
* at english_malay.my.apps.com.recordaudio.MainActivity.stopRecording(MainActivity.java:70)