首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MediaRecorder IOException:准备失败

MediaRecorder IOException:准备失败
EN

Stack Overflow用户
提问于 2013-02-01 14:08:42
回答 6查看 13K关注 0票数 6

我想用MediaRecorder录制语音,我的代码是:

代码语言:javascript
复制
 public void record(View v) {
       Log.d(TAG, "record");

    this.mediaRecorder.setAudioChannels(1);
    this.mediaRecorder.setAudioSamplingRate(44100);
    this.mediaRecorder.setAudioEncodingBitRate(64000);
    this.mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    this.mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    this.mediaRecorder.setOutputFile(this.file.getAbsolutePath());
    this.mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
    try {
        this.mediaRecorder.prepare();
        this.mediaRecorder.start();

        // update the buttons
        this.setButtonsEnabled(false, true, false);
    } catch (IOException e) {
        Log.e(TAG, "Failed to record()", e);
    }
}

代码语言:javascript
复制
   public void record(View v) {
    Log.d(TAG, "record");
    this.mediaRecorder = new MediaRecorder();
    this.mediaRecorder.setAudioChannels(1);
    this.mediaRecorder.setAudioSamplingRate(8000);

    this.mediaRecorder.setAudioEncodingBitRate(16);
    this.mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    this.mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
    this.mediaRecorder.setOutputFile(this.file.getAbsolutePath());

    this.mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

    try {
        this.mediaRecorder.prepare();
        this.mediaRecorder.start();

        // update the buttons
        this.setButtonsEnabled(false, true, false);
    } catch (IOException e) {
        Log.e(TAG, "Failed to record()", e);
    }
}

在三星电脑上,一切都可以,但在戴尔电脑上,两种方法都不能成功

下面是logcat:

代码语言:javascript
复制
 02-01 13:56:51.094: E/AudioRecorderDemoActivity(1397): Failed to record()
 02-01 13:56:51.094: E/AudioRecorderDemoActivity(1397): java.io.IOException: prepare failed.
 02-01 13:56:51.094: E/AudioRecorderDemoActivity(1397):     at android.media.MediaRecorder._prepare(Native Method)
 02-01 13:56:51.094: E/AudioRecorderDemoActivity(1397):     at android.media.MediaRecorder.prepare(MediaRecorder.java:524)
 02-01 13:56:51.094: E/AudioRecorderDemoActivity(1397):     at com.marakana.android.audiorecorderdemo.AudioRecorderDemoActivity.record(AudioRecorderDemoActivity.java:69)
 02-01 14:05:20.074: E/AndroidRuntime(1790): FATAL EXCEPTION: main
 02-01 14:05:20.074: E/AndroidRuntime(1790): java.lang.IllegalStateException: Could not execute method of the activity
EN

回答 6

Stack Overflow用户

发布于 2013-02-01 14:45:42

首先,你的代码看起来很好。是否已将所需权限添加到清单文件?

代码语言:javascript
复制
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />

如果是,请尝试替换:

代码语言:javascript
复制
this.mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);

通过

代码语言:javascript
复制
this.mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

不要忘记检查视频文件的路径是否正确。

票数 2
EN

Stack Overflow用户

发布于 2014-03-17 15:18:36

这是一个很大的问题,但有一个非常小的解决方案

在大多数情况下,我们从this.file.getAbsolutePath()获得的文件名包含file:///作为前缀

代码语言:javascript
复制
    ////////////////////////////////////////////////* INCORRECT CODE */
    this.mediaRecorder.setOutputFile(this.file.getAbsolutePath());
    /*the above line sets a file url beginning with a "file:///"
    //however, since this setOutputFile requires us to send a
    //string referring to the uri, we will have to get rid of the
    //"file:///" and simply write the uri */
    ////////////////////////////////////////////////* CORRECTED CODE BELOW */
    this.mediaRecorder.setOutputFile(this.file.getAbsolutePath().substring(8));
    /*the above line of code extracts the string uri eliminating
    // file:/// */

希望这个答案对你有帮助

票数 2
EN

Stack Overflow用户

发布于 2013-02-01 19:35:37

我删掉了

this.mediaRecorder.setAudioEncodingBitRate(16);

方法2,现在它起作用了。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14640734

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档