Android 비디오 녹화 기능 의 실현 절차

공식 사용 안내
비디오 녹화 기본 절차
1.설명 권한

 <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <--            SD ,         ->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
주의:RECORDAUDIO 는 위험 권한 으로,안 드 로 이 드 6.0 부터(API 레벨 23)시작 해 동적 으로 가 져 와 야 한다.
2.비디오 녹화 파라미터 설정

import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.text.TextUtils;

/**
 * @author Created by LRH
 * @description       (       )
 * @date 2021/1/19 13:54
 */
public class VideoRecorderConfig {
	//Camera
    private Camera camera;
    //       
    private int videoWidth;
    //       
    private int videoHeight;
    //         
    private int cameraRotation;
    //       
    private String path;
    //  Camera    SurfaceTexture,       SurfaceTexture
    //    SurfaceHolder
    private SurfaceTexture mSurfaceTexture;
    
    private int cameraId = 0;

    public SurfaceTexture getSurfaceTexture() {
        return mSurfaceTexture;
    }

    public void setSurfaceTexture(SurfaceTexture surfaceTexture) {
        mSurfaceTexture = surfaceTexture;
    }

    public Camera getCamera() {
        return camera;
    }

    public void setCamera(Camera camera) {
        this.camera = camera;
    }

    public int getVideoWidth() {
        return videoWidth;
    }

    public void setVideoWidth(int videoWidth) {
        this.videoWidth = videoWidth;
    }

    public int getVideoHeight() {
        return videoHeight;
    }

    public void setVideoHeight(int videoHeight) {
        this.videoHeight = videoHeight;
    }

    public int getCameraRotation() {
        return cameraRotation;
    }

    public void setCameraRotation(int cameraRotation) {
        this.cameraRotation = cameraRotation;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public int getCameraId() {
        return cameraId;
    }

    public void setCameraId(int cameraId) {
        this.cameraId = cameraId;
    }

    public boolean checkParam() {
        return mSurfaceTexture != null && camera != null && videoWidth > 0 && videoHeight > 0 && !TextUtils.isEmpty(path);
    }
}
3.비디오 녹화 인터페이스 봉인(사용Google 오디 오 및 비디오 가이드)

import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Build;
import android.view.Surface;

import com.emp.yjy.baselib.utils.LogUtils;
import java.io.IOException;

/**
 * @author Created by LRH
 * @description
 * @date 2021/1/19 13:53
 */
public class VideoRecorder {
    private static final String TAG = "VideoRecord";
    private MediaRecorder mRecorder;

    public VideoRecorder() {

    }

    /**
     *     
     *
     * @param config
     * @return
     */
    public boolean startRecord(VideoRecorderConfig config, MediaRecorder.OnErrorListener listener) {
        if (config == null || !config.checkParam()) {
            LogUtils.e(TAG, "    ");
            return false;
        }
        if (mRecorder == null) {
            mRecorder = new MediaRecorder();
        }
        mRecorder.reset();
        if (listener != null) {
            mRecorder.setOnErrorListener(listener);
        }

        config.getCamera().unlock();
        mRecorder.setCamera(config.getCamera());
        //      
//        mRecorder.setAudioChannels(1);
        //   
//        AudioSource.DEFAULT:      
//        AudioSource.MIC:   (  )
//        AudioSource.VOICE_UPLINK:    
//        AudioSource.VOICE_DOWNLINK:    
//        AudioSource.VOICE_CALL:  、    
//        AudioSource.CAMCORDER:        
//        AudioSource.VOICE_RECOGNITION:    
//        AudioSource.VOICE_COMMUNICATION:    
        mRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
        //   
        mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

        try {
            //              
            CamcorderProfile bestCamcorderProfile = getBestCamcorderProfile(config.getCameraId());
            mRecorder.setProfile(bestCamcorderProfile);
        } catch (Exception e) {
            //      
            mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
            //      
            mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
            //      
            mRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);
        }

        //       
        mRecorder.setVideoSize(config.getVideoWidth(), config.getVideoHeight());
//              
        mRecorder.setVideoFrameRate(30);
//        mRecorder.setAudioEncodingBitRate(44100);
//             (              )
        mRecorder.setVideoEncodingBitRate(800 * 1024);
//                 (           )
        mRecorder.setOrientationHint(config.getCameraRotation());
//                     (  )
        mRecorder.setMaxDuration(15 * 1000);
        //         
        mRecorder.setOutputFile(config.getPath());
        //      (    SurfaceHoler  )
        mRecorder.setPreviewDisplay(new Surface(config.getSurfaceTexture()));
        //   
        try {
            mRecorder.prepare();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        //    
        mRecorder.start();
        return true;
    }

    /**
     *     
     */
    public void stopRecord() {
        if (mRecorder != null) {
            try {
                mRecorder.stop();
                mRecorder.reset();
                mRecorder.release();
                mRecorder = null;
            } catch (Exception e) {
                e.printStackTrace();
                LogUtils.e(TAG, e.getMessage());
            }

        }
    }

    /**
     *     
     *
     * @return
     */
    public boolean pause() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && mRecorder != null) {
            mRecorder.pause();
            return true;
        }
        return false;
    }

    /**
     *     
     *
     * @return
     */
    public boolean resume() {
        if (mRecorder != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            mRecorder.resume();
            return true;
        }
        return false;
    }
}

public CamcorderProfile getBestCamcorderProfile(int cameraID){
		CamcorderProfile profile = CamcorderProfile.get(cameraID, CamcorderProfile.QUALITY_LOW);
		if(CamcorderProfile.hasProfile(cameraID, CamcorderProfile.QUALITY_480P)){
			//    720             
			profile = CamcorderProfile.get(cameraID, CamcorderProfile.QUALITY_480P);
			profile.videoBitRate = profile.videoBitRate/5;
			return profile;
		}
		if(CamcorderProfile.hasProfile(cameraID, CamcorderProfile.QUALITY_720P)){
			//    480             !!
			profile = CamcorderProfile.get(cameraID, CamcorderProfile.QUALITY_720P);
			profile.videoBitRate = profile.videoBitRate/35;
			return profile;
		}
		if(CamcorderProfile.hasProfile(cameraID, CamcorderProfile.QUALITY_CIF)){
			profile = CamcorderProfile.get(cameraID, CamcorderProfile.QUALITY_CIF);
			return profile;
		}
		if(CamcorderProfile.hasProfile(cameraID, CamcorderProfile.QUALITY_QVGA)){
			profile = CamcorderProfile.get(cameraID, CamcorderProfile.QUALITY_QVGA);
			return profile;
		}
		return profile;
	}
3.예시 사용

public void recordVideo(CustomCameraView CameraView) {
        VideoRecorderConfig config = new VideoRecorderConfig();
        String path = App.sGlobalContext.getExternalFilesDir("video").getAbsolutePath() + File.separator + "test1.mp4";
        config.setCamera(CameraView.getCamera());
        config.setCameraRotation(CameraView.getCameraRotation());
        config.setVideoHeight(CameraView.getCameraSize().height);
        config.setVideoWidth(CameraView.getCameraSize().width);
        config.setPath(path);
        config.setSurfaceTexture(CameraView.getSurfaceTexture());
        config.setCameraId(0);
        VideoRecorder record = new VideoRecorder();
        boolean start = record.startRecord(config, null);
        int duration = 15_000;
        while (duration > 0) {
            if (duration == 10_000) {
                boolean pause = record.pause();
                LogUtils.e(TAG, "    " + pause);
            }
            if (duration == 5_000) {
                boolean resume = record.resume();
                LogUtils.e(TAG, "      " + resume);
            }
            SystemClock.sleep(1_000);
            duration -= 1_000;
        }

        record.stopRecord();
        LogUtils.d(TAG, "    ");
    }
그 중CustomCameraView자신 을 위 한 카메라 라 이브 러 리
안 드 로 이 드 동 영상 녹화 기능 의 실현 절차 에 관 한 이 글 은 여기까지 소개 되 었 습 니 다.더 많은 안 드 로 이 드 동 영상 녹화 내용 은 우리 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 조회 하 시기 바 랍 니 다.앞으로 많은 응원 바 랍 니 다!

좋은 웹페이지 즐겨찾기