Improve MediaRecorderVideoRecorder

v2
Mattia Iavarone 6 years ago
parent 4b5d09ec55
commit fa576d7561
  1. 3
      cameraview/src/androidTest/java/com/otaliastudios/cameraview/VideoResultTest.java
  2. 27
      cameraview/src/main/java/com/otaliastudios/cameraview/MediaCodecVideoRecorder.java
  3. 11
      cameraview/src/main/java/com/otaliastudios/cameraview/MediaRecorderVideoRecorder.java
  4. 13
      cameraview/src/main/java/com/otaliastudios/cameraview/VideoResult.java

@ -31,6 +31,7 @@ public class VideoResultTest extends BaseTest {
boolean isSnapshot = true;
int maxDuration = 1234;
long maxFileSize = 500000;
int reason = VideoResult.REASON_MAX_DURATION_REACHED;
result.file = file;
result.rotation = rotation;
@ -40,6 +41,7 @@ public class VideoResultTest extends BaseTest {
result.isSnapshot = isSnapshot;
result.maxDuration = maxDuration;
result.maxSize = maxFileSize;
result.endReason = reason;
assertEquals(result.getFile(), file);
assertEquals(result.getRotation(), rotation);
@ -49,5 +51,6 @@ public class VideoResultTest extends BaseTest {
assertEquals(result.isSnapshot(), isSnapshot);
assertEquals(result.getMaxSize(), maxFileSize);
assertEquals(result.getMaxDuration(), maxDuration);
assertEquals(result.getTerminationReason(), reason);
}
}

@ -0,0 +1,27 @@
package com.otaliastudios.cameraview;
import android.hardware.Camera;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
/**
* A {@link VideoRecorder} that uses {@link android.media.MediaCodec} APIs.
*/
class MediaCodecVideoRecorder extends VideoRecorder {
private static final String TAG = MediaCodecVideoRecorder.class.getSimpleName();
private static final CameraLogger LOG = CameraLogger.create(TAG);
MediaCodecVideoRecorder(VideoResult stub, VideoResultListener listener, Camera camera, int cameraId) {
super(stub, listener);
}
@Override
void start() {
}
@Override
void close() {
}
}

@ -14,9 +14,11 @@ class MediaRecorderVideoRecorder extends VideoRecorder {
private MediaRecorder mMediaRecorder;
private CamcorderProfile mProfile;
private Mapper mMapper;
MediaRecorderVideoRecorder(VideoResult stub, VideoResultListener listener, Camera camera, int cameraId) {
super(stub, listener);
mMapper = new Mapper1();
mMediaRecorder = new MediaRecorder();
mMediaRecorder.setCamera(camera);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
@ -24,6 +26,8 @@ class MediaRecorderVideoRecorder extends VideoRecorder {
// TODO: should get a profile of a quality compatible with the chosen size.
}
// Camera2 constructor here...
@Override
void start() {
if (mResult.getAudio() == Audio.ON) {
@ -38,7 +42,7 @@ class MediaRecorderVideoRecorder extends VideoRecorder {
if (mResult.getCodec() == VideoCodec.DEFAULT) {
mMediaRecorder.setVideoEncoder(mProfile.videoCodec);
} else {
mMediaRecorder.setVideoEncoder(new Mapper1().map(mResult.getCodec()));
mMediaRecorder.setVideoEncoder(mMapper.map(mResult.getCodec()));
}
mMediaRecorder.setVideoEncodingBitRate(mProfile.videoBitRate);
if (mResult.getAudio() == Audio.ON) {
@ -61,7 +65,11 @@ class MediaRecorderVideoRecorder extends VideoRecorder {
public void onInfo(MediaRecorder mediaRecorder, int what, int extra) {
switch (what) {
case MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED:
mResult.endReason = VideoResult.REASON_MAX_DURATION_REACHED;
stop();
break;
case MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED:
mResult.endReason = VideoResult.REASON_MAX_SIZE_REACHED;
stop();
break;
}
@ -91,5 +99,6 @@ class MediaRecorderVideoRecorder extends VideoRecorder {
}
mProfile = null;
mMediaRecorder = null;
mMapper = null;
}
}

@ -11,6 +11,10 @@ import java.io.File;
*/
public class VideoResult {
public static final int REASON_USER = 0;
public static final int REASON_MAX_SIZE_REACHED = 1;
public static final int REASON_MAX_DURATION_REACHED = 2;
boolean isSnapshot;
Location location;
int rotation;
@ -20,6 +24,7 @@ public class VideoResult {
Audio audio;
long maxSize;
int maxDuration;
int endReason;
VideoResult() {}
@ -113,4 +118,12 @@ public class VideoResult {
public Audio getAudio() {
return audio;
}
/**
* Returns the reason why the recording was stopped.
* @return one of {@link #REASON_USER}, {@link #REASON_MAX_DURATION_REACHED} or {@link #REASON_MAX_SIZE_REACHED}.
*/
public int getTerminationReason() {
return endReason;
}
}

Loading…
Cancel
Save