我尝试使用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 public void onInfo(MediaRecorder mr, int what, int extra)中的OnErrorListener返回what=802和extra=6,我已经在真实设备上尝试过此代码,但不起作用。
发布于 2013-02-20 12:36:46
试试这个,它对我很有效:
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();发布于 2013-02-20 12:49:48
您可以尝试这样做:
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。
发布于 2014-05-19 17:33:39
这是MediaRecorder捕获媒体的一个很好的参考。http://developer.android.com/guide/topics/media/audio-capture.html
本教程中使用的示例代码无需任何更改即可运行。(只需确保您在清单文件中添加了必要的权限)
https://stackoverflow.com/questions/14972245
复制相似问题