1:权限
2:布局
<Button
android:id="@+id/startRecorder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="开始录制" />
<Button
android:id="@+id/stopRecorder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="停止录制" />
<Button
android:id="@+id/startPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="播放录音" />
<Button
android:id="@+id/stopPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="停止播放" />
3:代码
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button startRecorder;
private Button stopRecorder;
private Button startPlay;
private Button stopPlay;
private MediaPlayer mediaPlayer;
private MediaRecorder recorder;
private File audo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
startRecorder = (Button) findViewById(R.id.startRecorder);
stopRecorder = (Button) findViewById(R.id.stopRecorder);
startPlay = (Button) findViewById(R.id.startPlay);
stopPlay = (Button) findViewById(R.id.stopPlay);
startRecorder.setOnClickListener(this);
stopRecorder.setOnClickListener(this);
startPlay.setOnClickListener(this);
stopPlay.setOnClickListener(this);
mediaPlayer=new MediaPlayer();
//录音机
recorder=new MediaRecorder();
//录制原音
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
//输出格式
recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
//编码格式
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
//根目录下
File file = Environment.getExternalStorageDirectory();
audo=new File(file,"wangzeyun.amr");
try {
audo.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.startRecorder:
try {
recorder.setOutputFile(audo.getAbsolutePath());
recorder.prepare();
recorder.start();
} catch (IOException e) {
e.printStackTrace();
}
break;
case R.id.stopRecorder:
recorder.stop();
recorder.release();
break;
case R.id.startPlay:
try {
startPlay();
} catch (IOException e) {
e.printStackTrace();
}
break;
case R.id.stopPlay:
mediaPlayer.stop();
break;
}
}
private void startPlay() throws IOException {
//重置
mediaPlayer.reset();
mediaPlayer.setDataSource(audo.getAbsolutePath());
if (!mediaPlayer.isPlaying()){
mediaPlayer.prepare();
mediaPlayer.start();
}
else {
mediaPlayer.pause();
}
}
// 注意在onDestory中销毁、回收资源
@Override
protected void onDestroy() {
super.onDestroy();
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
// 释放资源
mediaPlayer.release();
recorder.release();
}
}