MediaRecorder不录制音频

时间:2013-02-20 04:21:55

标签: android audio-recording android-mediarecorder

我尝试使用MediaRecorder录制音频:

MediaRecorder recorder= new MediaRecorder();
recorder.reset();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);//
recorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(path);
recorder.setMaxDuration(30*60*1000);
recorder.prepare();
recorder.start();

代码正常运行时没有异常,有些时候文件没有在SDCard上创建,如果文件将创建,则文件大小为0KB。

我还注册了错误和信息听众OnInfoListener OnErrorListener OnInfoListener public void onInfo(MediaRecorder mr, int what, int extra)返回= 802和extra = 6 我在真实设备上试过这个代码,但没有用。

3 个答案:

答案 0 :(得分:0)

试试这个为我工作:

MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(path);
recorder.prepare();
recorder.start();

答案 1 :(得分:0)

你可以试试这个:

                mRecorder = new MediaRecorder();
                mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                mRecorder.setOutputFile(mFileName);
                mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

                try {
                    mRecorder.prepare();
                } catch (IOException e) {
                    Log.e("start", "prepare() failed");
                }

                mRecorder.start();

如果您收到任何错误,请粘贴logcat。

答案 2 :(得分:0)

这是MediaRecorder捕获媒体的绝佳参考。 http://developer.android.com/guide/topics/media/audio-capture.html

本教程中使用的示例代码无需任何更改即可运行。 (只需确保在清单文件中添加了必要的权限)