录音的步骤:
mRecAudioPath=Environment.getExternalStorageDirectory();
File mRecAudioFile=File.createTempFile(strTempFile, ".amr",mRecAudioPath);
mMediaRecorder=new MediaRecorder();
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);//Call this only before setOutputFormat().这里很重要,如果在setOutputFormat的后面调用的话,会报异常!
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
mMediaRecorder.setOutputFile(mRecAudioFile.getAbsolutePath());
mMediaRecorder.prepare();
mMediaRecorder.start();
采用上面的步骤运行后,后台报了异常:
ERROR/AndroidRuntime(287): java.lang.RuntimeException: setAudioSource failed.
原因:权限问题
在AndroidManifest.xml文件中添加:
<uses-permission
android:name="android.permission.RECORD_AUDIO">
</uses-permission>