State & Events
Every io.video.videokit.recorder.Recorder
implementation keeps its internal state and
offers a way to register observer objects to listen to important events.
Recorder state
The recorder state can be retrieved with recorder.state
and, similar to our player
component, matches one of the constants in the io.video.videokit.recorder.RecorderState
class:
State | Description |
---|---|
RecorderState.Busy | Recorder is busy in some operation or transitioning from one state to another. |
RecorderState.Idle | Recorder is showing the camera preview, not recording. |
RecorderState.Recording | Recorder is recording the camera preview. |
RecorderState.Preview | Recorder is in preview mode, previewing a previously recorded video. |
Recorder events
All Recorder
events are dispatched to a io.video.videokit.recorder.Recorder.Observer
that was
previously registered using addObserver
or similar function.
recorder.observeIn(lifecycle, observer) // start listening, stops automatically
recorder.observeIn(coroutineJob, observer) // start listening, stops automatically
recorder.addObserver(observer) // start listening, remember to remove later
recorder.removeObserver(observer) // stop listening
Observers are very important to be up-to-date about the recorder state, handle errors and create a responsive UI. We describe the observer interface as comments in the interface description below.
recorder.addObserver(object : Recorder.Observer {
override fun onResult(record: Record) {
// Called after confirming the record.
}
override fun onError(error: VideoKitException) {
// Something went wrong! Handle the error.
}
override fun onStateChanged(state: RecorderState) {
// State changed!
}
override fun onRecordingChanged(recording: Recording?) {
// If recording != null, we now have a pending recording.
// This happens after the first clip is started.
}
override fun onDurationChanged(duration: Long) {
// Duration has changed, as recording progresses.
}
})
Coroutines
For kotlinx.coroutines
users, note that all the reactive properties that are dispatched to the observers
are also available as a Flow
and StateFlow
.
recorder.stateFlow
.onEach { /* state changed */ }
.launchIn(scope)
recorder.errorFlow
.onEach { /* something went wrong */ }
.launchIn(scope)
// And more