Camera 录制视频的实现

使用 Camera 录制视频, 实现步骤如下:

需要权限: android.permission.CAMERA

android.permission.RECORD_AUDIO

android.permission.WRITE_EXTERNAL_STORAGE

如果录制前后需要预览效果, 需要在录制开始前停止预览, 并解锁摄像头.

录制停止后锁上摄像头, 再开始预览.

MediaRecorder mMediaRecorder = new MediaRecorder();

mMediaRecorder.setCamera(camera);// 关联摄像头

mMediaRecorder.setAudioSource(AudioSource.CAMCORDER);// 设置音频源

mMediaRecorder.setVideoSource(VideoSource.CAMERA);// 设置视频源

CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_LOW);// 获得摄像质量低的配置

mMediaRecorder.setProfile(profile);// 设置配置文件

mMediaRecorder.setOutputFile(“/mnt/sdcard/test.mp4”);// 设置输出文件.

mMediaRecorder.setPreviewDisplay(mHolder.getSurface());// 设置预览显示

mMediaRecorder.prepare();// 准备

mMediaRecorder.start();// 录制

mMediaRecorder.stop();// 停止

mMediaRecorder.release();// 释放资源


public class MainActivity extends Activity implements Callback {
   private Camera camera;
   private SurfaceHolder mHolder;
   private MediaRecorder mMediaRecorder;
   private boolean isCapturing = false;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main3);   
      SurfaceView surfaceView = (SurfaceView) findViewById(R.id.surfaceview);
      mHolder = surfaceView.getHolder();
      // 设置数据来源自外部, 不维护自己内部的缓冲区, 而是等待渲染引擎把数据推送到界面上. 
      // 注意: 4.0以前版本必须要设置, 4.0以后版本自动会设置.
      mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
      mHolder.addCallback(this);

  }
   public void capture(View v) {
      if(!isCapturing) {
        isCapturing = true;     
         camera.stopPreview();
         camera.unlock();
         mMediaRecorder = new MediaRecorder();
         mMediaRecorder.setCamera(camera);
         mMediaRecorder.setAudioSource(AudioSource.CAMCORDER);
         mMediaRecorder.setVideoSource(VideoSource.CAMERA);   
         CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_LOW);
         mMediaRecorder.setProfile(profile);     
         File sdcardFile = Environment.getExternalStorageDirectory();
         String path = new File(sdcardFile, "video" + System.currentTimeMillis() + ".mp4").getPath();
         mMediaRecorder.setOutputFile(path);
         mMediaRecorder.setPreviewDisplay(mHolder.getSurface());
         try {
            mMediaRecorder.prepare();
            mMediaRecorder.start();
         } catch (Exception e) {

           e.printStackTrace();
         }
      } else {
         isCapturing = false;
         mMediaRecorder.stop();
         mMediaRecorder.release();
         mMediaRecorder = null;
         camera.lock();
         camera.startPreview();
      }
   }
   @Override
   public void surfaceCreated(SurfaceHolder holder) {

      try {
         camera = Camera.open();
         camera.setPreviewDisplay(mHolder);
         camera.startPreview();
      } catch (IOException e) {
        e.printStackTrace();
      }
   }

   @Override
   public void surfaceChanged(SurfaceHolder holder, int format, int width,
         int height) {
      // TODO Auto-generated method stub
   }
   @Override
   public void surfaceDestroyed(SurfaceHolder holder) {
      camera.stopPreview();
      camera.release();
      camera = null;
   }
}

 

posted on 2017-02-20 21:24  LoaderMan  阅读(1422)  评论(0编辑  收藏  举报

导航