You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
3.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.record;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.media.MediaRecorder;
import android.os.Environment;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
@SuppressLint("Registered")
public class RecordActivity extends Activity implements View.OnClickListener {
Button startRecording, stopRecording, pauseRecording;//开始录音、停止录音、释放资源
MediaRecorder recorder;
File audioFile; //录音保存的文件
boolean isRecoding = false;// true 表示正在录音
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
init();
initListener();
}
//初始化
public void init() {
stopRecording = (Button) this.findViewById(R.id.btn_stop);
startRecording = (Button) this.findViewById(R.id.btn_play);
pauseRecording = (Button) this.findViewById(R.id.btn_pause);
if (!path.exists()) {
path.mkdirs();
}
}
//初始化麦克风
private void InitMedia(){
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);//设置播放源 麦克风
recorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB); //设置输入格式 3gp
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); //设置编码AMR
}
//设置监听器
public void initListener() {
startRecording.setOnClickListener(this);
stopRecording.setOnClickListener(this);
pauseRecording.setOnClickListener(this);
}
//此处还应该对手机进行下判断,判断下手机里面有没有内存卡
//保存在SD卡下MediaRecorderTest文件夹中
File path = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/MediaRecorderTest");
//录音
public void recod() {
InitMedia();
int i = (int) (Math.random() * 10000);
String a = Integer.toString(i);
String b = "Voice";
String FileName = b + a;
try {
audioFile = new File(path, FileName + ".aac");
//audioFile=new File(path,"ycc.aac");
if (audioFile.exists()) {
audioFile.delete();
}
audioFile.createNewFile();//创建文件
} catch (Exception e) {
throw new RuntimeException("Couldn't create recording audio file", e);
}
recorder.setOutputFile(audioFile.getAbsolutePath()); //设置输出文件
try {
recorder.prepare();
} catch (IllegalStateException e) {
throw new RuntimeException("IllegalStateException on MediaRecorder.prepare", e);
} catch (IOException e) {
throw new RuntimeException("IOException on MediaRecorder.prepare", e);
}
isRecoding = true;
recorder.start();
}
public void onClick(View v) {
//开始录音
while (true){
if (v == startRecording) {
Toast.makeText(RecordActivity.this, "开始录音", Toast.LENGTH_SHORT).show();
recod();
break;
}
if (v == pauseRecording) {
}
//停止录音
if (v == stopRecording) {
if (isRecoding) {
Toast.makeText(RecordActivity.this, "录音结束,文件已存至/MediaRecorderTest", Toast.LENGTH_SHORT).show();
recorder.stop();//调用stop之后recorder会被清空再次录音要再次初始化recorder
break;
}
}
}
}
}