1.界面布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:onClick="start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始录制" />
<Button
android:id="@+id/button2"
android:onClick="stop"
android:enabled="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止录制" />
<Button
android:id="@+id/button3"
android:onClick="play"
android:enabled="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="播放录音" />
<Button
android:id="@+id/button4"
android:onClick="delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除录音文件" />
</LinearLayout>
2.添加权限及注册Serivce
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<service android:name="com.swust.audiocapture.MyService"></service>
3.主程序编写
public class MainActivity extends Activity {
private Button startBT;
private Button stopBT;
private Button playBT;
private Intent service;
private boolean isRecording;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startBT = (Button) findViewById(R.id.button1);
stopBT = (Button) findViewById(R.id.button2);
playBT = (Button) findViewById(R.id.button3);
service = new Intent(this,MyService.class);
isRecording = false;
}
public void start(View v){
isRecording = true;
setBtEnable(isRecording);
startService(service);
}
public void stop(View v){
isRecording = false;
setBtEnable(isRecording);
stopService(service);
}
private void setBtEnable(boolean flag) {
startBT.setEnabled(!flag);
stopBT.setEnabled(flag);
playBT.setEnabled(!flag);
}
public void play(View v){
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file:///mnt/sdcard/records.3gp"),"audio/*");
startActivity(intent);
}
public void delete(View v){
File file = new File("/mnt/sdcard/records.3gp");
try {
if (file.exists()) {
if (file.delete()) {
Toast.makeText(getApplicationContext(), "删除成功", 0).show();
}
} else {
Toast.makeText(getApplicationContext(), "文件不存在", 0).show();
}
} catch (Exception e) {
// TODO: handle exception
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
outState.putBoolean("isRecording", isRecording);
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
isRecording = savedInstanceState.getBoolean("isRecording");
setBtEnable(isRecording);
super.onRestoreInstanceState(savedInstanceState);
}
}
4.录音服务Service编写
public class MyService extends Service {
private MediaRecorder recorder;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC); //设置音频源 麦克风
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);// 设置输出格式 3GP
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);//设置编码
recorder.setOutputFile("/mnt/sdcard/records.3gp");//设置文件路径
try {
recorder.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//准备
recorder.start();//开始
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
recorder.stop();//停止录音
recorder.release();//释放
recorder = null;
super.onDestroy();
}
}