MainActivity.java
package com.example.soundrecord_demo;import java.io.IOException;import android.media.MediaRecorder;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity implements OnClickListener{ private Button btn1, btn2; private MediaRecorder record; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn1 = (Button) findViewById(R.id.button1); btn2 = (Button) findViewById(R.id.button2); btn1.setOnClickListener(this); btn2.setOnClickListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public void onClick(View arg0) { // TODO Auto-generated method stub switch(arg0.getId()) { case R.id.button1: initAudio(); break; case R.id.button2: record.stop(); record.reset(); record.release(); break; default: break; } } @SuppressWarnings("deprecation") private void initAudio() { record = new MediaRecorder(); record.setAudioSource(MediaRecorder.AudioSource.MIC); // 设置MediaRecorder的音频源为麦克风 record.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR); // 设置MediaRecorder录制的音频格式 record.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); // 设置MediaRecorder录制音频的编码为amr. record.setOutputFile("/sdcard/richard.amr"); // 设置录制好的音频文件保存路径 try { record.prepare(); record.start(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
activity_main.xml
strings.xml
SoundRecord_demo Settings play stop
AndroidManifest.xml