您的位置:首页 > 移动开发 > Android开发

android开发,静音录制视频,在一般清晰度的前提下保证文件大小越小越好

2015-09-09 15:20 726 查看
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);   //设置编码比特率,不设置会使视频图像模糊
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: