我知道这个问题曾多次询问过。但我仍然无法找到原因;他们都没有帮助解决这个问题。
我正在尝试使用Android MediaRecorder录制音频片段,并希望播放它。但是我在下面的代码行中遇到了IllegalStateException。
recorder.start();
这是我的代码:
private void startRecording() {
try {
mFileName = Environment.getExternalStorageDirectory()
.getAbsolutePath();
mFileName += "/audio_recording.3gp";
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
// recorder.setOutputFile(fos.getFD());
recorder.setOutputFile(mFileName);
try {
recorder.prepare();
recorder.start();
} catch (IllegalStateException e) {
System.out.println("Error in preparing recorder!!! IllegelStateException");
}catch (IOException e) {
System.out.println("Error in preparing recorder!!!");
}
} catch (IOException e) {
System.out.println("IOException caught during recording!!! IO Exception");
e.printStackTrace();
}
}
错误日志:
02-17 19:09:40.086: E/AndroidRuntime(25620): FATAL EXCEPTION: main
02-17 19:09:40.086: E/AndroidRuntime(25620): java.lang.IllegalStateException: Could not execute method of the activity
02-17 19:09:40.086: E/AndroidRuntime(25620): at android.view.View$1.onClick(View.java:3103)
02-17 19:09:40.086: E/AndroidRuntime(25620): at android.view.View.performClick(View.java:3574)
02-17 19:09:40.086: E/AndroidRuntime(25620): at android.view.View$PerformClick.run(View.java:14293)
02-17 19:09:40.086: E/AndroidRuntime(25620): at android.os.Handler.handleCallback(Handler.java:605)
02-17 19:09:40.086: E/AndroidRuntime(25620): at android.os.Handler.dispatchMessage(Handler.java:92)
02-17 19:09:40.086: E/AndroidRuntime(25620): at android.os.Looper.loop(Looper.java:137)
02-17 19:09:40.086: E/AndroidRuntime(25620): at android.app.ActivityThread.main(ActivityThread.java:4448)
02-17 19:09:40.086: E/AndroidRuntime(25620): at java.lang.reflect.Method.invokeNative(Native Method)
02-17 19:09:40.086: E/AndroidRuntime(25620): at java.lang.reflect.Method.invoke(Method.java:511)
02-17 19:09:40.086: E/AndroidRuntime(25620): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
02-17 19:09:40.086: E/AndroidRuntime(25620): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
02-17 19:09:40.086: E/AndroidRuntime(25620): at dalvik.system.NativeStart.main(Native Method)
02-17 19:09:40.086: E/AndroidRuntime(25620): Caused by: java.lang.reflect.InvocationTargetException
02-17 19:09:40.086: E/AndroidRuntime(25620): at java.lang.reflect.Method.invokeNative(Native Method)
02-17 19:09:40.086: E/AndroidRuntime(25620): at java.lang.reflect.Method.invoke(Method.java:511)
02-17 19:09:40.086: E/AndroidRuntime(25620): at android.view.View$1.onClick(View.java:3098)
02-17 19:09:40.086: E/AndroidRuntime(25620): ... 11 more
02-17 19:09:40.086: E/AndroidRuntime(25620): Caused by: java.lang.RuntimeException: start failed.
02-17 19:09:40.086: E/AndroidRuntime(25620): at android.media.MediaRecorder.start(Native Method)
02-17 19:09:40.086: E/AndroidRuntime(25620): at com.example.audiorecordtest.MainActivity.startRecording(MainActivity.java:146)
02-17 19:09:40.086: E/AndroidRuntime(25620): at com.example.audiorecordtest.MainActivity.onRecord(MainActivity.java:94)
02-17 19:09:40.086: E/AndroidRuntime(25620): at com.example.audiorecordtest.MainActivity.onSayButtonClick(MainActivity.java:66)
02-17 19:09:40.086: E/AndroidRuntime(25620): ... 14 more
我也宣布了所需的权限。
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
我该如何解决这个问题?