android 录音异常setAudioSource failed

来源:互联网 发布:淘宝修图师招聘 编辑:程序博客网 时间:2024/04/27 17:05

录音的步骤:

   
    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>