当我按住按钮时,我想用MediaRecorder
开始录制语音留言。我在尝试以IllegalStateException
开始录制时收到onLongClickListener
。
我收到的错误如堆栈跟踪中所述,位于第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)
答案 0 :(得分:2)
您忘记添加<uses-permission android:name="android.permission.RECORD_AUDIO" />
权限以在Manifest.xml
文件上录制音频。
修改:您的onTouchListener
正在干扰onLongClickListener
,可能会在初始化之后以及启动之前停止MediaRecorder
,甚至在录音机为空时停止它。这会为您的Exception
提供原点。
请注意,如果您对该行view.onTouchEvent(event);
发表评论,它会开始录制,但根据用户操作,该行为具有不可预测的行为。
请注意,Event.ACTION_CANCEL
可能会在您的方案中发生,因此必须考虑recorder
才能停止recorder
。
在致电stop()
之前,请务必检查{{1}}是否为空。