android MediaRecorder錄制視頻

MediaRecorder可以實現(xiàn)直接錄制視頻,不能對每一幀數(shù)據(jù)進行處理。如果對每一幀數(shù)據(jù)都能進行處理的話,有兩種選擇: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:質(zhì)量等級對應于最高可用分辨率*/
            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);

            /*攝像頭默認是橫屏,這是拍攝的視頻旋轉(zhuǎn)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));可以直接設置錄制視頻的質(zhì)量:


視頻質(zhì)量列表.png

視頻質(zhì)量的分辨率: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();
        }
    }
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容