gpt4 book ai didi

android - 在 onLongClickListener 中调用 MediaRecorder.start 时在 android.media.MediaRecorder.start 处出现 java.lang.IllegalStateException

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:16:12 28 4
gpt4 key购买 nike

我想在按住按钮时使用 MediaRecorder 开始录制语音消息。尝试在 onLongClickListener 中开始录制时出现 IllegalStateException

如堆栈跟踪中所述,我收到的错误位于第 219 行:recorder.start()

为什么会这样?

btnSendVoice.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
isVoiceButtonHeld = true;
startRecording();
return false;
}
});

btnSendVoice.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
view.onTouchEvent(event);

if (event.getAction() == MotionEvent.ACTION_UP) {
if (isVoiceButtonHeld) {
isVoiceButtonHeld = false;
stopRecording();
}
}

return false;
}
});

private void startRecording() {
Toast.makeText(getActivity(), "Recording Message", Toast.LENGTH_SHORT).show();

filename = Environment.getExternalStorageDirectory().getAbsolutePath() + "/audiotest.3gp";

recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setOutputFile(filename);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

try { recorder.prepare(); }
catch (IOException e) { Log.e("MediaRecorder", "prepare() failed"); }
recorder.start();
}

private void stopRecording() {
Toast.makeText(getActivity(), "Recording Finished", Toast.LENGTH_SHORT).show();

recorder.stop();
recorder.release();
recorder = null;
}

@Override
public void onPause() {
super.onPause();

// Free up resources from MediaRecorder when leaving Fragment
if (recorder != null) {
recorder.release();
recorder = null;
}
}

LogCat

07-15 16:24:32.256: E/MediaRecorder(4227): start failed: -38
07-15 16:24:32.256: D/AndroidRuntime(4227): Shutting down VM
07-15 16:24:32.256: W/dalvikvm(4227): threadid=1: thread exiting with uncaught exception (group=0x41551ba8)
07-15 16:24:32.406: E/AndroidRuntime(4227): FATAL EXCEPTION: main
07-15 16:24:32.406: E/AndroidRuntime(4227): Process: com.walintukai.lfdate, PID: 4227
07-15 16:24:32.406: E/AndroidRuntime(4227): java.lang.IllegalStateException
07-15 16:24:32.406: E/AndroidRuntime(4227): at android.media.MediaRecorder.start(Native Method)
07-15 16:24:32.406: E/AndroidRuntime(4227): at com.walintukai.lfdate.ChatFragment.startRecording(ChatFragment.java:219)
07-15 16:24:32.406: E/AndroidRuntime(4227): at com.walintukai.lfdate.ChatFragment.access$4(ChatFragment.java:206)
07-15 16:24:32.406: E/AndroidRuntime(4227): at com.walintukai.lfdate.ChatFragment$3.onLongClick(ChatFragment.java:132)
07-15 16:24:32.406: E/AndroidRuntime(4227): at android.view.View.performLongClick(View.java:4474)
07-15 16:24:32.406: E/AndroidRuntime(4227): at android.view.View$CheckForLongPress.run(View.java:18418)
07-15 16:24:32.406: E/AndroidRuntime(4227): at android.os.Handler.handleCallback(Handler.java:733)
07-15 16:24:32.406: E/AndroidRuntime(4227): at android.os.Handler.dispatchMessage(Handler.java:95)
07-15 16:24:32.406: E/AndroidRuntime(4227): at android.os.Looper.loop(Looper.java:136)
07-15 16:24:32.406: E/AndroidRuntime(4227): at android.app.ActivityThread.main(ActivityThread.java:5050)
07-15 16:24:32.406: E/AndroidRuntime(4227): at java.lang.reflect.Method.invokeNative(Native Method)
07-15 16:24:32.406: E/AndroidRuntime(4227): at java.lang.reflect.Method.invoke(Method.java:515)
07-15 16:24:32.406: E/AndroidRuntime(4227): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
07-15 16:24:32.406: E/AndroidRuntime(4227): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
07-15 16:24:32.406: E/AndroidRuntime(4227): at dalvik.system.NativeStart.main(Native Method)

最佳答案

您忘记添加 <uses-permission android:name="android.permission.RECORD_AUDIO" />允许在您的 Manifest.xml 上录制音频文件。

编辑:您的 onTouchListener正在干扰 onLongClickListener并且可能会停止你的 MediaRecorder在它初始化之后和开始之前,甚至在记录器为空时停止它。这为您的 Exception 提供了来源.

请注意,如果您注释行 view.onTouchEvent(event); ,它开始录制,但根据用户操作,从那一刻起它就会出现不可预测的行为。

请注意 Event.ACTION_CANCEL有可能发生在你的场景中,必须考虑到你的 recorder停了。

确保检查您的 recorder在调用 stop() 之前不为空.

关于android - 在 onLongClickListener 中调用 MediaRecorder.start 时在 android.media.MediaRecorder.start 处出现 java.lang.IllegalStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24769833/

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