我目前正在嘗試長時間錄製音頻並按住。當我點擊並按住該應用程序時,該應用程序仍然有效,但是,當我單擊該按鈕時,該應用程序會崩潰。請幫我弄清楚爲什麼會發生這種情況。應用程序使用onTouch,崩潰onClick
p/s:另外,有人可以告訴我如何格式化日誌顯示在這裏?我很難與
代碼
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主:
<?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)
好,它的工作原理!最後一件事,你能告訴我如何在開始錄製之前延遲或者任何鏈接都可以做到。謝謝你,Atef Hares。 –
@ S.k.Surain不用客氣。你可以使用這個[answer](https://stackoverflow.com/a/9166354/5993410)這是一個簡單的方法來延遲執行一些代碼。 –
我也很好奇。它從技術上調用Action_down(它又調用startRecording())是如何不記錄的?再次感謝 –