Skip to content

Instantly share code, notes, and snippets.

@mayowa-egbewunmi
Created July 31, 2022 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mayowa-egbewunmi/6103f7e3816c34811a586107cf95106b to your computer and use it in GitHub Desktop.
Save mayowa-egbewunmi/6103f7e3816c34811a586107cf95106b to your computer and use it in GitHub Desktop.
class VideoCaptureManager private constructor(private val builder: Builder) :
LifecycleEventObserver {
...
@SuppressLint("MissingPermission")
fun startRecording(filePath: String) {
val outputOptions = FileOutputOptions.Builder(File(filePath)).build()
activeRecording = videoCapture.output
.prepareRecording(getContext(), outputOptions)
.apply { withAudioEnabled() }
.start(ContextCompat.getMainExecutor(getContext()), videoRecordingListener)
}
fun pauseRecording() {
activeRecording.pause()
listener?.recordingPaused()
}
fun resumeRecording() {
activeRecording.resume()
}
fun stopRecording() {
activeRecording.stop()
}
private val videoRecordingListener = Consumer<VideoRecordEvent> { event ->
when (event) {
is VideoRecordEvent.Finalize -> if (event.hasError()) {
listener?.onError(event.cause)
} else {
listener?.recordingCompleted(event.outputResults.outputUri)
}
is VideoRecordEvent.Pause -> listener?.recordingPaused()
is VideoRecordEvent.Status -> {
listener?.onProgress(event.recordingStats.recordedDurationNanos)
}
}
}
interface Listener {
fun onInitialised(cameraLensInfo: HashMap<Int, CameraInfo>)
fun onProgress(progress: Int)
fun recordingPaused()
fun recordingCompleted(outputUri: Uri)
fun onError(throwable: Throwable?)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment