`
chjmars
  • 浏览: 76074 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Android 音频录音

阅读更多

检查SDcard是否存在:

 

private boolean checkSDCard() {
	if (Environment.MEDIA_MOUNTED.equals(Environment
		.getExternalStorageState()) && Environment.getExternalStorageDirectory().canWrite()) {
		return true;
	} else
		return false;
}

 录音:

 

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;
	}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics