Video recording start and stop#
Camera 1#
When can I start or stop recording ?#
Video recording can be started only when all of the following conditions are met:
camera is active:
camera.isActive == true
camera mode is recording:
camera.modeSetting.mode == .recording
recording state is stopped or one of the error states:
camera.recordingState.functionState
Video recording can be stopped when the following condition is true:
recording state is started:
camera.recordingState.functionState == .started
The Camera API provides helper properties telling whether recording can be started are stopped:
camera.canStartRecord
: tells whether recording can be startedcamera.canStopRecord
: tells whether recording can be stopped
Sample code to monitor whether video recording can be started or stopped :
/// Keep reference on MainCamera peripheral to get notified of changes.
private var cameraRef: Ref<MainCamera>?
/// Monitors and prints whether video recording can be started or stopped.
func monitorCanStartStopRecord(drone: Drone) {
cameraRef = drone.getPeripheral(Peripherals.mainCamera) { camera in
// called on main thread when the camera peripheral changes
if let camera = camera {
if camera.canStartRecord {
print("Video recording can be started")
} else if camera.canStopRecord {
print("Video recording can be stopped")
} else {
print("Video recording can't be started or stopped")
}
}
}
}
How to start or stop recording ?#
Sample code to start and stop video recording:
/// Starts or stops video recording.
func toggleStartStopRecord(drone: Drone) {
if let camera = drone.getPeripheral(Peripherals.mainCamera) {
if camera.canStartRecord {
// recording can be started
camera.startRecording()
} else if camera.canStopRecord {
// recording can be stopped
camera.stopRecording()
} else {
// recording can't be started or stopped
print("Video recording can't be started or stopped")
}
}
}
Camera 2#
When can I start or stop recording ?#
Video recording can be started only when all of the following conditions are met:
camera is active:
camera.isActive == true
camera mode is recording:
camera.config[Camera2Params.mode]?.value == .recording
recording state is stopped:
camera.getComponent(Camera2Components.recording)?.state.canStart == true
Video recording can be stopped when the following condition is true:
recording state is started or starting:
camera.getComponent(Camera2Components.recording)?.state.canStop == true
Sample code to monitor whether video recording can be started or stopped :
/// Keep reference on Camera2Recording component to get notified of changes.
private var recordingRef: Ref<Camera2Recording>?
/// Monitors and prints whether video recording can be started or stopped.
func monitorCanStartStopRecord(drone: Drone) {
if let camera = drone.getPeripheral(Peripherals.mainCamera2) {
// get recording component reference and register listener
recordingRef = camera.getComponent(Camera2Components.recording) { recording in
if recording?.state.canStart == true {
print("Video recording can be started")
} else if recording?.state.canStop == true {
print("Video recording can be stopped")
} else {
print("Video recording can't be started or stopped")
}
}
}
}
How to start or stop recording ?#
Sample code to start and stop video recording:
/// Starts or stops video recording.
func toggleStartStopRecord(drone: Drone) {
if let camera = drone.getPeripheral(Peripherals.mainCamera2),
let recording = camera.getComponent(Camera2Components.recording) {
if recording.state.canStart {
// recording can be started
recording.start()
} else if recording.state.canStop {
// recording can be stopped
recording.stop()
} else {
// recording state don't allow to start or stop
print("Video recording can't be started or stopped")
}
} else {
// camera peripheral or recording component not available at present
print("Video recording not available")
}
}