为什么MediaRecorder的start()方法抛出IllegalStateException? [英] Why does the start() method of MediaRecorder throw an IllegalStateException?
本文介绍了为什么MediaRecorder的start()方法抛出IllegalStateException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试录制音频,但是 MediaRecorder
类的 start()
方法抛出 IllegalStateException
.我使用以下代码:
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();
并获得许可
<uses-permission android:name="android.permission.RECORD_AUDIO" />
推荐答案
维杰,recorder.setOutputFile("/sdcard/");正在设置目录,而不是文件.替换为:
Vijay, recorder.setOutputFile("/sdcard/"); is setting a directory, not a file. Replace that with:
mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
mFileName += "/youraudiofile.3gp";
使用"/sdcard"硬编码一条易碎的路径,因此请使用上面的
Using "/sdcard" hard codes a path which is fragile, so use the above
此外,要使其正常工作,您必须添加
Also, for this to work you must add
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
到您的AndroidManifext.xml
to your AndroidManifext.xml
这篇关于为什么MediaRecorder的start()方法抛出IllegalStateException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文