gpt4 book ai didi

java - 应用程序使用onTouch,在单击时崩溃

转载 作者:行者123 更新时间:2023-12-01 16:48:03 27 4
gpt4 key购买 nike

我目前正在尝试长按并按住来录制音频。当我点击并按住时,应用程序可以运行,但是,当我单击按钮时,应用程序会崩溃。请帮我弄清楚为什么会发生这种情况。

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)

最佳答案

这只是因为您试图停止非录音录音机!

您应该在停止录音机之前进行检查。

  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");
}
isRecording = true;
mRecorder.start();
}

和:

private void stopRecording() {
try{
if(mRecorder!=null && isRecording){
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
}catch (Exception e){//handle this exception}
}
<小时/>

此外,您还应该在以下时间开始录制之前添加一些延迟时间(例如 1 秒):action == MotionEvent.ACTION_DOWN,以便能够区分点击单击并按住

关于java - 应用程序使用onTouch,在单击时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46026258/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com