我正在尝试使用MediaRecorder在我的一个Activity中录制音频。部分代码如下。
File file = new File(AppConstants.MSGS_DIR, filename);
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(AudioSource.MIC);
recorder.setOutputFormat(OutputFormat.THREE_GPP);
recorder.setAudioEncoder(AudioEncoder.AMR_WB);
recorder.setOutputFile(file.getAbsolutePath());
try {
recorder.prepare();
recorder.start();
} catch (IOException e) {
System.out.println("Exception: " + e.getMessage());
}
我在清单文件中授予了以下权限。
(Note: 保留了HTML标签)
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
但是在 recorder.start() 这一行代码处,我遇到了运行时异常。Logcat 显示以下错误信息。
09-23 15:47:54.462: E/AndroidRuntime(8697): FATAL EXCEPTION: main
09-23 15:47:54.462: E/AndroidRuntime(8697): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mypackage/com.mypackage.RecordingActivity}: java.lang.RuntimeException: start failed.
09-23 15:47:54.462: E/AndroidRuntime(8697): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2250)
09-23 15:47:54.462: E/AndroidRuntime(8697): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2300)
09-23 15:47:54.462: E/AndroidRuntime(8697): at android.app.ActivityThread.access$600(ActivityThread.java:144)
09-23 15:47:54.462: E/AndroidRuntime(8697): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1295)
09-23 15:47:54.462: E/AndroidRuntime(8697): at android.os.Handler.dispatchMessage(Handler.java:99)
09-23 15:47:54.462: E/AndroidRuntime(8697): at android.os.Looper.loop(Looper.java:150)
09-23 15:47:54.462: E/AndroidRuntime(8697): at android.app.ActivityThread.main(ActivityThread.java:5162)
09-23 15:47:54.462: E/AndroidRuntime(8697): at java.lang.reflect.Method.invokeNative(Native Method)
09-23 15:47:54.462: E/AndroidRuntime(8697): at java.lang.reflect.Method.invoke(Method.java:525)
09-23 15:47:54.462: E/AndroidRuntime(8697): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:744)
09-23 15:47:54.462: E/AndroidRuntime(8697): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-23 15:47:54.462: E/AndroidRuntime(8697): at dalvik.system.NativeStart.main(Native Method)
09-23 15:47:54.462: E/AndroidRuntime(8697): Caused by: java.lang.RuntimeException: start failed.
09-23 15:47:54.462: E/AndroidRuntime(8697): at android.media.MediaRecorder.start(Native Method)
09-23 15:47:54.462: E/AndroidRuntime(8697): at com.mypackage.RecordingActivity.startRecording(RecordingActivity.java:169)
09-23 15:47:54.462: E/AndroidRuntime(8697): at com.mypackage.RecordingActivity.onCreate(RecordingActivity.java:107)
09-23 15:47:54.462: E/AndroidRuntime(8697): at android.app.Activity.performCreate(Activity.java:5288)
09-23 15:47:54.462: E/AndroidRuntime(8697): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-23 15:47:54.462: E/AndroidRuntime(8697): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2214)
我尝试在stackoverflow上寻找与错误对应的问题,但无法从中得出有效的答案。请检查一下这个代码,告诉我哪里错了。
顺便说一下,这段代码只在特定的设备上失败了。我是否遗漏了额外的权限?
AudioEncoder.AMR_WB
仅从API10开始提供。 - ozbekContext.getExternalFilesDir()
)? - ozbek