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
자신 을 위 한 카메라 라 이브 러 리안 드 로 이 드 동 영상 녹화 기능 의 실현 절차 에 관 한 이 글 은 여기까지 소개 되 었 습 니 다.더 많은 안 드 로 이 드 동 영상 녹화 내용 은 우리 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 조회 하 시기 바 랍 니 다.앞으로 많은 응원 바 랍 니 다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Bitrise에서 배포 어플리케이션 설정 테스트하기이 글은 Bitrise 광고 달력의 23일째 글입니다. 자체 또는 당사 등에서 Bitrise 구축 서비스를 사용합니다. 그나저나 며칠 전 Bitrise User Group Meetup #3에서 아래 슬라이드를 발표했...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.