public void startRecord() { mediarecorder = new MediaRecorder();// 創建mediarecorder對象 mCamera = getCameraInstance(); Parameters parameters = mCamera.getParameters(); mCamera.autoFocus(null); // 解鎖camera mCamera.setDisplayOrientation(90); mCamera.unlock(); mediarecorder.setCamera(mCamera); List<Size> supportedPreviewSizes = parameters.getSupportedPreviewSizes(); for(int i=0;i<supportedPreviewSizes.size();i++) { Log.v("startRecord", "width="+supportedPreviewSizes.get(i).width+";height="+supportedPreviewSizes.get(i).height); } // 設置錄制視頻源為Camera(相機) // mediarecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER); mediarecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); // 設置錄制文件質量,格式,分辨率之類,這個全部包括了 // mediarecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_480P)); //7.43M 10frame // mediarecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_1080P)); //70.94M 10frame // mediarecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_CIF)); // 2.6M 5frame/10frame // mediarecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_QCIF)); //0.76M 30frame 模糊 // mediarecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_QVGA)); //2.1M // mediarecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_TIME_LAPSE_CIF)); //不支持 // mediarecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_LOW)); //766KB 還行 比QUALITY_QCIF好 // mediarecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_TIME_LAPSE_LOW)); //1M 質量類似LOW // mediarecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_TIME_LAPSE_480P)); //480p效果 // mediarecorder.setAudioEncoder(MediaRecorder.AudioEncoder.); // mediarecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); // boolean isSupQUALITY_TIME_LAPSE_CIF = CamcorderProfile.hasProfile(CamcorderProfile.QUALITY_TIME_LAPSE_CIF); // boolean isSupQUALITY_LOW = CamcorderProfile.hasProfile(CamcorderProfile.QUALITY_LOW); // Log.v("startRecord", "isSupQUALITY_TIME_LAPSE_CIF="+isSupQUALITY_TIME_LAPSE_CIF+";isSupQUALITY_LOW="+isSupQUALITY_LOW); // // mediarecorder.setVideoFrameRate(30); //start實現錄像靜音 mediarecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); // mediarecorder.setVideoSize(640,480); mediarecorder.setVideoSize(1280,720); //設置編碼比特率,不設置會使視頻圖像模糊 // mediarecorder.setVideoEncodingBitRate(5*1024*1024); //清晰 512*1024(不清楚) mediarecorder.setVideoEncodingBitRate(900*1024); //較為清晰,且文件大小為3.26M(30秒) mediarecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); //H263的貌似有點不清晰 // mediarecorder.setVideoFrameRate(10); //設置無效 //end mediarecorder.setPreviewDisplay(surfaceHolder.getSurface()); // 設置視頻文件輸出的路徑 mediarecorder.setOutputFile(Environment.getExternalStorageDirectory().getAbsolutePath() + "/data/data/"+System.currentTimeMillis()+".mp4"); try { // 准備錄制 mediarecorder.prepare(); // 開始錄制 mediarecorder.start(); } catch (Exception e) { e.printStackTrace(); } }
一般情況下,我們直接調用 mediarecorder 會產生很大的視頻文件,30秒的為70M,因此,我們需要在錄制視頻時盡量控制視頻文件的大小!
mediarecorder.setVideoEncodingBitRate(900*1024); //設置編碼比特率,不設置會使視頻圖像模糊