码迷,mamicode.com
首页 > 移动开发 > 详细

Android开发之视频录制1

时间:2015-06-05 13:46:12      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

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

Android开发之视频录制1

标签:

原文地址:http://www.cnblogs.com/HuiLove/p/4554279.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!