最近在做语音IVR的时候需要对通话进行录音,主要用到了MediaRecorder类,实现代码如下:
MediaRecorder mRecorder = new MediaRecorder();mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),"ivr_record");if(!dir.exists()){dir.mkdirs();}mFileName = new File(dir,"ivr_"+System.currentTimeMillis()+".3gp").getAbsolutePath();Log.i(TAG, "start recording:"+mFileName);mRecorder.setOutputFile(mFileName);mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);try {mRecorder.prepare();} catch (IOException e) {Log.e(TAG, "prepare() failed");}mRecorder.start();
这种方式只能录自己的语音,对方的语音录不到,如果想录对方的语音,只需要将audio source设置成 MediaRecorder.AudioSource.VOICE_CALL就可以了,代码如下:
MediaRecorder mRecorder = new MediaRecorder();mRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),"ivr_record");if(!dir.exists()){dir.mkdirs();}mFileName = new File(dir,"ivr_"+System.currentTimeMillis()+".3gp").getAbsolutePath();Log.i(TAG, "start recording:"+mFileName);mRecorder.setOutputFile(mFileName);mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);try {mRecorder.prepare();} catch (IOException e) {Log.e(TAG, "prepare() failed");}mRecorder.start();