Android开发之视频录制1

Android开发之视频录制

 (2011-10-18 17:47:46)
标签: 

android

 

视频录制

 

surfaceview

 

杂谈

分类: 3GAndroid学习笔记
要使用视频录制功能,要用到MediaRecorder这个类,以及SurfaceView组建
public class MainActivity extends Activity {
public static final String TAG = "MainActivity";
   @Override
protected void onDestroy() {
  mediaRecorder.release();
super.onDestroy();
}
   private SurfaceView surfaceView;
   private Button btn_start;
   private Button btn_stop;
   private MediaRecorder mediaRecorder;
   private boolean record;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mediaRecorder = new MediaRecorder();
        surfaceView = (SurfaceView) findViewById(R.id.surfaceview);
       
        this.surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        this.surfaceView.getHolder().setFixedSize(320, 240);//设置分辨率
        btn_start = (Button) findViewById(R.id.btn_start);
        btn_stop=(Button) findViewById(R.id.btn_stop);
        ClickListener clickListener = new ClickListener();
        btn_start.setOnClickListener(clickListener);
        btn_stop.setOnClickListener(clickListener);
    }
    private final class ClickListener implements View.OnClickListener{
public void onClick(View v) {
if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
Toast.makeText(MainActivity.this, "SD卡不存在或写保护!", 1).show();
return ;
}
    try {
switch(v.getId()){
case R.id.btn_start:
mediaRecorder.reset();
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); //从照相机采集视频
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setVideoSize(320, 240);
mediaRecorder.setVideoFrameRate(3); //每秒3帧
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263); //设置视频编码方式
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
File videoFile = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis()+".3gp");
mediaRecorder.setOutputFile(videoFile.getAbsolutePath());
mediaRecorder.setPreviewDisplay(surfaceView.getHolder().getSurface());
mediaRecorder.prepare();//预期准备
mediaRecorder.start();//开始刻录
record = true;
break;
case R.id.btn_stop:
if(record){
mediaRecorder.stop();
record = false;
}
break;
}
} catch (Exception e) {
Log.e(TAG, e.toString());
}
}
   
    }
 
    }

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。