MediaRecorder 类录制音频时出现问题 - 准备()给出异常 - 权限被拒绝
我是 Android 开发新手,我有下一个问题。
我正在使用 MediaRecorder 类来仅录制来自麦克风的音频。我按照官方网站中指示的步骤操作: http://developer. android.com/reference/android/media/MediaRecorder.html
所以我有一个方法可以初始化和配置 MediaRecorder 对象以开始录制。这里有代码:
//initializes audio recorder
MediaRecorder mrecorder = new MediaRecorder();
//configure the input sources
mrecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
//set the output format
mrecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
//set the audio encoding
mrecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
//specify the output file
mrecorder.setOutputFile("/sdcard/test.3gp");
//prepare for recording
try {
mrecorder.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
Log.d("Syso". e.toString());
} catch (IOException e) {
e.printStackTrace();
Log.d("Syso". e.toString());
}
当我在模拟器中执行此代码时,多亏了 logcat,我可以看到方法 prepare() 在调用时给出了异常:
java.io.FileNotFoundException: /sdcard/test.3gp (Permission denied)
我不知道为什么会发生这种情况。由于异常消息,我通过在 xml 中添加以下行来在清单中授予访问存储的权限:
<uses-permission android:name="android.permission.STORAGE" />
但这并不能解决任何问题,而且我仍然始终遇到相同的异常。 SDCard是根据模拟器安装的,所以我不知道。
I'm new in Android development and I have the next question/problem.
I'm playing around with the MediaRecorder class to record just audio from the microphone. I'm following the steps indicated in the official site: http://developer.android.com/reference/android/media/MediaRecorder.html
So I have a method that initializes and configure the MediaRecorder object in order to start recording. Here you have the code:
//initializes audio recorder
MediaRecorder mrecorder = new MediaRecorder();
//configure the input sources
mrecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
//set the output format
mrecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
//set the audio encoding
mrecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
//specify the output file
mrecorder.setOutputFile("/sdcard/test.3gp");
//prepare for recording
try {
mrecorder.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
Log.d("Syso". e.toString());
} catch (IOException e) {
e.printStackTrace();
Log.d("Syso". e.toString());
}
When I execute this code in the simulator, thanks to logcat, I can see that the method prepare() gives an exception when is called:
java.io.FileNotFoundException: /sdcard/test.3gp (Permission denied)
And I have no idea why this is happening. Due to the message of the exception, I've given permissions in the manifest to access to storage by adding the following line to the xml:
<uses-permission android:name="android.permission.STORAGE" />
But this doesn't fix anything and I still get the same exception all the time. The SDCard is mounted according to the emulator, so I have no clue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 WRITE_EXTERNAL_STORAGE 权限添加到 AndroidManifest.xml。
Add the WRITE_EXTERNAL_STORAGE permission to AndroidManifest.xml.