MediaRecorder 类录制音频时出现问题 - 准备()给出异常 - 权限被拒绝

发布于 2024-09-04 00:10:19 字数 1537 浏览 5 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独自唱情﹋歌 2024-09-11 00:10:19

WRITE_EXTERNAL_STORAGE 权限添加到 AndroidManifest.xml。

Add the WRITE_EXTERNAL_STORAGE permission to AndroidManifest.xml.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文