Android实现录音功能总结方案:
一.通过Intent方式实现:
private int RESULT_CAPTURE_RECORDER_SOUND = 1001;
1.实现录音的方法:
private void soundRecorderMethod() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("audio/amr");
startActivityForResult(intent, RESULT_CAPTURE_RECORDER_SOUND);
}
2.在onActivityResult()中获取录音数据
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
if(resultCode == RESULT_OK) {
Uri uriRecorder = data.getData();
Cursor cursor=this.getContentResolver().query(uriRecorder, null, null, null, null);
if (cursor.moveToNext()) {
/** _data:文件的绝对路径 ,_display_name:文件名 */
strRecorderPath = cursor.getString(cursor.getColumnIndex("_data"));
Toast.makeText(this, strRecorderPath, Toast.LENGTH_SHORT).show();
}
}
}
}
二.使用MediaRecorder实现
private boolean isStopRecord; // 是否停止录音
1.start()方法:
private void start(String dir, String fileName){
try{
File myRecAudioFile = new File(dir, fileName);
mMediaRecorder = new MediaRecorder();
// 设置录音为麦克风
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
//录音文件保存这里
mMediaRecorder.setOutputFile(myRecAudioFile.getAbsolutePath());
mMediaRecorder.prepare();
mMediaRecorder.start();
//mMediaRecorder.getMaxAmplitude();
//mMediaRecorder.getAudioSourceMax();
mMediaRecorder.setOnInfoListener(new MediaRecorder.OnInfoListener() {
@Override
public void onInfo(MediaRecorder mr, int what, int extra) {
int a = mr.getMaxAmplitude();
Toast.makeText(EX07.this, a, Toast.LENGTH_SHORT).show();
}
});
}
isStopRecord = false;
}catch(Exception e){
}
}
2.stop()方法
private void stop(){
if (mMediaRecorder != null && !isStopRecord) {
// 停止录音
mMediaRecorder.stop();
mMediaRecorder.release();
mMediaRecorder = null;
}
}
3.播放
private void openFile(File f) {
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
String type = getMIMEType(f);
intent.setDataAndType(Uri.fromFile(f), type);
startActivity(intent);
// Uri uri=Uri.fromFile(f);
// MediaPlayer mediaPlayer=MediaPlayer.create(this, uri);
// try {
// mediaPlayer.prepare();
// } catch (IllegalStateException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// }
// mediaPlayer.start();
}
4.getMIMEType
private String getMIMEType(File f) {
String end = f.getName().substring(f.getName().lastIndexOf(".") + 1, f.getName().length()).toLowerCase();
String type = "";
if (end.equals("mp3") || end.equals("aac") || end.equals("amr") || end.equals("mpeg") || end.equals("mp4")) {
type = "audio";
} else if (end.equals("jpg") || end.equals("gif") || end.equals("png") || end.equals("jpeg")) {
type = "image";
} else {
type = "*";
}
type += "/";
return type;
}
5.在Activity的生命周期,stop时关闭录音资源
/**
* activity的生命周期,stop时关闭录音资源
*/
@Override
protected void onStop() {
if (mMediaRecorder != null && !isStopRecord) {
// 停止录音
mMediaRecorder.stop();
mMediaRecorder.release();
mMediaRecorder = null;
}
super.onStop();
}
三.