AVCaptureSession: interrupt event
The interrupt event fires when the system interrupts the session. While interrupted, the session temporarily stops capturing.
Syntax
session.addEventListener('interrupt', (event) => { })Event object
AVCaptureSessionInterruptEvent, with one extra field:
Why the session was interrupted: a string, one of these six; null when the system gives no reason:
| Value | Meaning |
|---|---|
'videoDeviceNotAvailableInBackground' | The app went to the background |
'audioDeviceInUseByAnotherClient' | Another app is using the microphone |
'videoDeviceInUseByAnotherClient' | Another app is using the camera |
'videoDeviceNotAvailableWithMultipleForegroundApps' | More than one app is in the foreground |
'videoDeviceNotAvailableDueToSystemPressure' | The system took the camera back for a reason such as the device being too hot |
'sensitiveContentMitigationActivated' | The system's sensitive content protection took effect |
target is the session. Not cancelable; preventDefault() has no effect.
When it fires
- On the turn after the system interrupts the session. At that point
isInterruptedistrue. - When the interruption ends,
interruptendfires.
Whether this event actually fires, the situations in which each reason appears, and what the preview layer looks like while interrupted are all not verified on a device.
Examples
const session = new AVCaptureSession();
session.addEventListener('interrupt', (e) => {
if (e.reason === 'videoDeviceInUseByAnotherClient') {
console.log('Another app is using the camera');
} else {
console.log('Interrupted', e.reason);
}
});Effect (not verified on a device): prints a line when the session is interrupted. When another app is using the camera, prints Another app is using the camera.
Corresponding iOS API
The notification AVCaptureSessionWasInterruptedNotification. userInfo[AVCaptureSessionInterruptionReasonKey] is e.reason; AVCaptureSessionInterruptionReasonVideoDeviceInUseByAnotherClient is written 'videoDeviceInUseByAnotherClient', and so on.
See also
interruptend: the interruption endedisInterrupted- When the session is interrupted