State & Events
Every RecorderProtocol
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 RecorderState
enum:
State | Description |
---|---|
RecorderState.busy(Recording?) | Recorder is busy in some operation or transitioning from one state to another. The argument represents the pending recording, if any. |
RecorderState.idle(Recording?) | Recorder is showing the camera preview, not recording. The argument represents the pending recording, if any. |
RecorderState.recording(Recording) | Recorder is recording the camera preview. The argument represents the pending recording. |
Recorder events
All RecorderProtocol
events are dispatched to a RecorderObserver
that was
previously registered using addObserver
.
recorder.addObserver(observer) // start listening (weak reference)
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.
class CustomRecorderObserver : RecorderObserver {
func recorderResult(_ result: Recording) {
// Called after confirming the record.
}
func recorderError(_ error: Error) {
// Something went wrong! Handle the error.
}
func recorderStateChanged(_ state: RecorderState) {
// State changed!
}
func recorderRecordingChanged(_ recording: Recording?) {
// If recording != null, we now have a pending recording.
// This happens after the first clip is started.
}
func recorderDurationChanged(_ duration: TimeInterval) {
// Duration has changed, as recording progresses.
}
})