美文网首页
android MediaRecorder录制视频

android MediaRecorder录制视频

作者: 钉某人 | 来源:发表于2018-08-14 10:55 被阅读0次

MediaRecorder可以实现直接录制视频,不能对每一帧数据进行处理。如果对每一帧数据都能进行处理的话,有两种选择:1.MediaCodec 2.FFMpeg_x264/openh264.

录制视频

 /**
     * 开始录制视频
     */
    public void startRecordVideo(){

        if(mCamera == null){
            Log.e(TAG,"Camera为null");
            return;
        }

        Camera.Parameters parameters = mCamera.getParameters();
        parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
        mCamera.setParameters(parameters);
        mCamera.startPreview();

        if (mMediaRecorder == null){
            mMediaRecorder = new MediaRecorder();
        }else {
            mMediaRecorder.reset();
        }

        try {

            /*1.解锁相机,为MediaRecorder设置相机*/
            mCamera.unlock();
            mMediaRecorder.setCamera(mCamera);

            /*2.设置音频源和视频源*/
            mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

            /*3.CamcorderProfile.QUALITY_HIGH:质量等级对应于最高可用分辨率*/
            mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_720P));

            /*设置视频的输出格式*/
//            mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);

            /*设置音频的编码格式*/
//            mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);

            /*设置视频的编码格式*/
//            mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);

            /*设置要捕获的视频的帧速率*/
//            mMediaRecorder.setVideoFrameRate(30);

            /*设置编码比特率*/
//            mMediaRecorder.setVideoEncodingBitRate( 5 * 1024 * 1024);

//            mMediaRecorder.setVideoSize(720,1280);

            /*4.设置输出文件*/
            String dirPath = Environment.getExternalStorageDirectory()+"/DCIM/Camera/";
            File dirFile = new File(dirPath);
            if (!dirFile.exists()) dirFile.mkdirs();
            mVideoFilePath = dirPath +"VIDEO_"+ System.currentTimeMillis()+".mp4";
            mMediaRecorder.setOutputFile(mVideoFilePath);

            /*摄像头默认是横屏,这是拍摄的视频旋转90度*/
            mMediaRecorder.setOrientationHint(90);

            /*5.设置预览输出*/
            mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());

            /*6.准备配置*/
            mMediaRecorder.prepare();

            /*7.开始录制*/
            mMediaRecorder.start();


        }catch (Exception e){
            e.printStackTrace();
        }
    }

mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_720P));可以直接设置录制视频的质量:


视频质量列表.png

视频质量的分辨率:CamcorderProfile.java

    /**
     * Quality level corresponding to the 720p (1280 x 720) resolution.
     */
    public static final int QUALITY_720P = 5;

    /**
     * Quality level corresponding to the 1080p (1920 x 1080) resolution.
     * Note that the vertical resolution for 1080p can also be 1088,
     * instead of 1080 (used by some vendors to avoid cropping during
     * video playback).
     */
    public static final int QUALITY_1080P = 6;

结束录制

    /**
     * 结束录制
     */
    public void stopRecordVideo(){
        if (mMediaRecorder != null){
            mMediaRecorder.stop();
            mMediaRecorder.release();
            getContext().sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,Uri.parse(mVideoFilePath)));
            Toast.makeText(getContext(),"视频保存在:"+mVideoFilePath,Toast.LENGTH_SHORT).show();
        }
    }

相关文章

网友评论

      本文标题:android MediaRecorder录制视频

      本文链接:https://www.haomeiwen.com/subject/bepmbftx.html