Android录制视频的流程,前面初始化和拍照流程一样,参考拍照流程中的First,Second;
Third: 初始化MediaRecorder
MediaRecorder = new MediaRecorder();
//Step1: Unlock and set camera to MediaRecorder
//mCamera.unLock();
//mMediaRecorder.setCamera(mCamera);
以上可用于设置前后摄像头
//Step 2: Set sources
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
//Step3 : Set a CamcorderProfile
mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_LOW));
//Step4: Set output file
mMediaRecorder.setOutputFile(filePath);
//Step5: Set the preview output.
mMediaRecorder.setPreviewDisplay(mHolder.getSurface());
此时如果surface创建成功,就可以:
mMediaRecorder.prepare();
mMediaRecorder.start();
在录制中途失败或者结束的时候需要释放MediaRecorder,并且释放摄像头:
mMediaRecorder.stop();
mMediaRecorder.reset();
mMediaRecorder.release();
mMediaRecorder = null;
在上述的Step3中,如果需要设置不同分辨率参数进行拍照,就需要做改动,Step3中应改成如下:
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setVideoSize(width,height);
//mMediaRecorder.setVideoFrameRate(3);
mMediaRecorder.setVideoEncorder(MediaRecorder.VideoEncorder.H264);
mMediaRecorder.setAudioEncorder(MediaRecorder.AudioEncorder.AMR_NB);
早在在录制视频的过程中,如果设置的是不支持的分辨率或者Surface未创建成功,将会产生start failed异常,即在调用mMediaRecorder.start()时。