Android 简单录音程序

1.界面布局


 Android 简单录音程序_第1张图片  Android 简单录音程序_第2张图片


 Android 简单录音程序_第3张图片



    
2.添加权限及注册Serivce


 
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();
	}
}






你可能感兴趣的:(Android,学习笔记)