Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add features: pause and resume recording video #789

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ allprojects {

ext {
compileSdkVersion = 29
minSdkVersion = 15
minSdkVersion = 18
targetSdkVersion = 29
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,14 @@ public void onVideoRecordingEnd() {

}

@UiThread
public void onVideoRecordingPause() {

}

@UiThread
public void onVideoRecordingResume() {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -1834,6 +1834,27 @@ public void run() {
});
}

public void pauseVideoRecording() {
mCameraEngine.pauseVideoRecording();
mUiHandler.post(new Runnable() {
@Override
public void run() {
if (getKeepScreenOn() != mKeepScreenOn) setKeepScreenOn(mKeepScreenOn);
}
});
}


public void resumeVideoRecording() {
mCameraEngine.resumeVideoRecording();
mUiHandler.post(new Runnable() {
@Override
public void run() {
if (getKeepScreenOn() != mKeepScreenOn) setKeepScreenOn(mKeepScreenOn);
}
});
}

/**
* Sets the max width for snapshots taken with {@link #takePictureSnapshot()} or
* {@link #takeVideoSnapshot(File)}. If the snapshot width exceeds this value, the snapshot
Expand Down Expand Up @@ -2082,6 +2103,11 @@ public boolean isTakingVideo() {
return mCameraEngine.isTakingVideo();
}


public boolean isRecordingPaused() {
return mCameraEngine.isRecordingPaused();
}

/**
* Returns true if the camera is currently capturing a picture
* @return boolean indicating if the camera is capturing a picture
Expand Down Expand Up @@ -2380,6 +2406,30 @@ public void run() {
}
});
}

@Override
public void dispatchOnVideoRecordingPause() {
mUiHandler.post(new Runnable() {
@Override
public void run() {
for (CameraListener listener : mListeners) {
listener.onVideoRecordingPause();
}
}
});
}

@Override
public void dispatchOnVideoRecordingResume() {
mUiHandler.post(new Runnable() {
@Override
public void run() {
for (CameraListener listener : mListeners) {
listener.onVideoRecordingResume();
}
}
});
}
}

//endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,16 @@ public void onVideoResult(@Nullable VideoResult.Stub result, @Nullable Exception
}
}

@Override
public void onVideoRecordingPause() {
setVideoRecordingPauseCallback();
}

@Override
public void onVideoRecordingResume() {
setVideoRecordingResumeCallback();
}

//endregion

//region Parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import android.media.Image;
import android.media.ImageReader;
import android.os.Build;
import android.util.Log;
import android.util.Pair;
import android.util.Range;
import android.util.Rational;
Expand Down Expand Up @@ -966,6 +967,22 @@ public void onVideoRecordingEnd() {
}
}

@Override
public void onVideoRecordingPause() {
Log.d(TAG, "onVideoRecordingPause: ");
setVideoRecordingPauseCallback();


}

@Override
public void onVideoRecordingResume() {
Log.d(TAG, "onVideoRecordingResume: ");
setVideoRecordingResumeCallback();
}



@Override
public void onVideoResult(@Nullable VideoResult.Stub result, @Nullable Exception exception) {
super.onVideoResult(result, exception);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,13 @@ public final boolean isTakingVideo() {
return mVideoRecorder != null && mVideoRecorder.isRecording();
}


@Override
public final boolean isRecordingPaused() {
return mVideoRecorder != null && mVideoRecorder.isRecordingPaused();
}


@Override
public final void takeVideo(final @NonNull VideoResult.Stub stub,
final @Nullable File file,
Expand Down Expand Up @@ -644,6 +651,57 @@ protected void onStopVideo() {
}
}

@Override
public final void pauseVideoRecording() {
getOrchestrator().schedule("pause video", true, new Runnable() {
@Override
public void run() {
// LOG.i("pauseVideoRecording", "running. isTakingVideo?", isTakingVideo());
onPauseVideoRecording();
}
});
}

@EngineThread
@SuppressWarnings("WeakerAccess")
protected void onPauseVideoRecording() {
if (mVideoRecorder != null) {
mVideoRecorder.pauseVideoRecording();
}
}


protected void setVideoRecordingPauseCallback() {
getCallback().dispatchOnVideoRecordingPause();
}


@Override
public final void resumeVideoRecording() {
getOrchestrator().schedule("resume video", true, new Runnable() {
@Override
public void run() {
// LOG.i("pauseVideoRecording", "running. isTakingVideo?", isTakingVideo());
onResumeVideoRecording();
}
});
}


@EngineThread
@SuppressWarnings("WeakerAccess")
protected void onResumeVideoRecording() {
if (mVideoRecorder != null) {
mVideoRecorder.resumeVideoRecording();
}
}

protected void setVideoRecordingResumeCallback() {
getCallback().dispatchOnVideoRecordingResume();
}



@CallSuper
@Override
public void onVideoResult(@Nullable VideoResult.Stub result, @Nullable Exception exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ void dispatchOnExposureCorrectionChanged(float newValue, @NonNull float[] bounds
void dispatchError(CameraException exception);
void dispatchOnVideoRecordingStart();
void dispatchOnVideoRecordingEnd();
void dispatchOnVideoRecordingPause();
void dispatchOnVideoRecordingResume();
}

protected static final String TAG = CameraEngine.class.getSimpleName();
Expand Down Expand Up @@ -714,11 +716,14 @@ public abstract void startAutoFocus(@Nullable Gesture gesture,
public abstract void takePictureSnapshot(final @NonNull PictureResult.Stub stub);

public abstract boolean isTakingVideo();
public abstract boolean isRecordingPaused();
public abstract void takeVideo(@NonNull VideoResult.Stub stub,
@Nullable File file,
@Nullable FileDescriptor fileDescriptor);
public abstract void takeVideoSnapshot(@NonNull VideoResult.Stub stub, @NonNull File file);
public abstract void stopVideo();
public abstract void pauseVideoRecording();
public abstract void resumeVideoRecording();

//endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Build;
import android.util.Log;

import com.otaliastudios.cameraview.CameraLogger;
import com.otaliastudios.cameraview.VideoResult;
Expand Down Expand Up @@ -346,4 +348,29 @@ protected void onStop(boolean isCameraShutdown) {
dispatchResult();
}


@Override
protected void onPauseVideoRecording() {
Log.d(TAG, "onPauseVideoRecording: ");
if (mMediaRecorder != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
mMediaRecorder.pause();
dispatchVideoRecordingPause();
}

}
}


@Override
protected void onResumeVideoRecording() {
Log.d(TAG, "onPauseVideoRecording: ");
if (mMediaRecorder != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
mMediaRecorder.resume();
dispatchVideoRecordingResume();
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.graphics.SurfaceTexture;
import android.opengl.EGL14;
import android.os.Build;
import android.util.Log;

import com.otaliastudios.cameraview.CameraLogger;
import com.otaliastudios.cameraview.internal.DeviceEncoders;
Expand Down Expand Up @@ -103,6 +104,7 @@ protected void onStop(boolean isCameraShutdown) {
}
}


@RendererThread
@Override
public void onRendererTextureCreated(int textureId) {
Expand Down Expand Up @@ -339,4 +341,16 @@ public void onEncodingEnd(int stopReason, @Nullable Exception e) {
}
dispatchResult();
}


@Override
protected void onPauseVideoRecording() {
// not supported
}

@Override
protected void onResumeVideoRecording() {
// not supported
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,17 @@ public interface VideoResultListener {
* and soon {@link #onVideoResult(VideoResult.Stub, Exception)} will be called.
*/
void onVideoRecordingEnd();

void onVideoRecordingPause();

void onVideoRecordingResume();

}

private final static int STATE_IDLE = 0;
private final static int STATE_RECORDING = 1;
private final static int STATE_STOPPING = 2;
private final static int STATE_PAUSING = 3;

@VisibleForTesting(otherwise = VisibleForTesting.PROTECTED) VideoResult.Stub mResult;
private final VideoResultListener mListener;
Expand Down Expand Up @@ -105,7 +111,15 @@ public final void stop(boolean isCameraShutdown) {
public boolean isRecording() {
// true if not idle.
synchronized (mStateLock) {
return mState != STATE_IDLE;
return mState != STATE_IDLE && mState!=STATE_PAUSING;
}
}


public boolean isRecordingPaused() {
// true if not idle.
synchronized (mStateLock) {
return mState ==STATE_PAUSING;
}
}

Expand Down Expand Up @@ -173,4 +187,50 @@ protected void dispatchVideoRecordingEnd() {
mListener.onVideoRecordingEnd();
}
}


public final void pauseVideoRecording() {
synchronized (mStateLock) {
if (mState == STATE_IDLE) {
return;
}
LOG.i("pause:", "Changed state to STATE_PAUSING");
mState = STATE_PAUSING;
}
onPauseVideoRecording();
}

protected abstract void onPauseVideoRecording();

public final void resumeVideoRecording() {
synchronized (mStateLock) {
if (mState == STATE_IDLE) {
return;
}
LOG.i("resume:", "Changed state to STATE_RECORDING");
mState = STATE_RECORDING;
}
onResumeVideoRecording();
}

protected abstract void onResumeVideoRecording();

@SuppressWarnings("WeakerAccess")
@CallSuper
protected void dispatchVideoRecordingPause() {
LOG.i("dispatchVideoRecordingPause:", "pause");
if (mListener != null) {
mListener.onVideoRecordingPause();
}
}

@SuppressWarnings("WeakerAccess")
@CallSuper
protected void dispatchVideoRecordingResume() {
LOG.i("dispatchVideoRecordingResume:", "resume");
if (mListener != null) {
mListener.onVideoRecordingResume();
}
}

}
Loading