为什么MediaRecorder的start()方法会抛出IllegalStateException?
我正在尝试录制音频,但 MediaRecorder
类的 start()
方法抛出 IllegalStateException
。 我使用以下代码:
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile("/sdcard/");
try {
recorder.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i("Try","Exception");
recorder.start();
和以下权限
<uses-permission android:name="android.permission.RECORD_AUDIO" />
I am trying to record audio but the start()
method of MediaRecorder
class throws an IllegalStateException
.
I use the following code:
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile("/sdcard/");
try {
recorder.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i("Try","Exception");
recorder.start();
And following permission
<uses-permission android:name="android.permission.RECORD_AUDIO" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
recorder.setOutputFile("/sdcard/");
设置的是目录,而不是文件。将其替换为:
使用“/sdcard”对脆弱的路径进行硬编码,因此请使用上面的内容。
另外,要使其工作,您必须添加
到 AndroidManifest.xml
recorder.setOutputFile("/sdcard/");
is setting a directory, not a file.Replace that with:
Using "/sdcard" hard codes a path which is fragile, so use the above.
Also, for this to work you must add
to your AndroidManifest.xml
当未调用
MediaRecorder.prepare
方法、或在MediaRecorder.start
之后调用、或在配置音视频源之前调用时,会抛出IllegalstateException
,格式和编码器。Android文档中相机开发指南中提到的正确配置顺序:
IllegalstateException
is thrown when theMediaRecorder.prepare
method is not called, or called afterMediaRecorder.start
, or called before configuring audio/video sources, format and encoders.The correct order of configuration mentioned in camera developer guide in the Android documentation: