|
本里使用MediaRecorder实现音频录音的功能: public void recording() {
if (checkSDCard()) {
mRecAudioPath = Environment.getExternalStorageDirectory();
File fPath = new File(mRecAudioPath.getPath() + File.separator
+ "WJ_RecordList");
fPath.mkdirs();
mRecAudioFile = fPath;
} else {
Toast.makeText(this.getContext(), "请插入SDcard", 0).show();
return ;
}
try {
mRecAudioFile = File.createTempFile(String.valueOf("tmp_record"), ".3gp", mRecAudioFile);
} catch (IOException e) {
Log.e("Jarvis", "mRec", e);
}
// instance
mRecorder = new MediaRecorder();
// 设置麦克风
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
// 输出文件格式
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
// 音频文件编码
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
// 输出文件路径
mRecorder.setOutputFile(mRecAudioFile.getAbsolutePath());
Log.e("Jarvis", "输出文件路径:" + mRecAudioFile.getAbsolutePath());
// 准备--开始
try {
mRecorder.prepare();
mRecorder.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void stopRec() {
if(mRecorder != null) {
mRecorder.stop();
mRecAudioFile.getName();
Log.e("Jarvis", "Name:" + mRecAudioFile.getName()+ "~" + mRecAudioFile.getAbsolutePath());
mRecorder.release();
mRecorder = null;
}
} |
|