AVCaptureSession
An object that configures capture behavior and coordinates the flow of data from input devices to capture outputs.
Declaration
class AVCaptureSessionMentioned in
Overview
To perform real-time capture, you instantiate a capture session and add appropriate inputs and outputs. The following code fragment illustrates how to configure a capture device to record audio.
// Create the capture session.
let captureSession = AVCaptureSession()
// Find the default audio device.
guard let audioDevice = AVCaptureDevice.default(for: .audio) else { return }
do {
// Wrap the audio device in a capture device input.
let audioInput = try AVCaptureDeviceInput(device: audioDevice)
// If the input can be added, add it to the session.
if captureSession.canAddInput(audioInput) {
captureSession.addInput(audioInput)
}
} catch {
// Configuration failed. Handle error.
}Call the startRunning() method to start the flow of data from the inputs to the outputs, and call the stopRunning() method to stop the flow.
You use the sessionPreset property to customize the quality level, bitrate, or other settings for the output. Most common capture configurations are available through session presets; however, some specialized options (such as high frame rate) require directly setting a capture format on an AVCaptureDevice instance.
Topics
Configuring a session
Setting a session preset
Configuring inputs
Configuring outputs
Connecting inputs and outputs
connectionsaddConnection(_:)canAddConnection(_:)addInputWithNoConnections(_:)addOutputWithNoConnections(_:)removeConnection(_:)AVCaptureAudioChannel
Configuring deferred start
isManualDeferredStartSupportedautomaticallyRunsDeferredStartrunDeferredStartWhenNeeded()deferredStartDelegatedeferredStartDelegateCallbackQueuesetDeferredStartDelegate(_:deferredStartDelegateCallbackQueue:)AVCaptureSessionDeferredStartDelegate
Configuring capture controls
supportsControlsmaxControlsCountcontrolscanAddControl(_:)addControl(_:)removeControl(_:)setControlsDelegate(_:queue:)AVCaptureSessionControlsDelegatecontrolsDelegatecontrolsDelegateCallbackQueue
Managing the session life cycle
Observing session state
isRunningisInterrupteddidStartRunningNotificationdidStopRunningNotificationwasInterruptedNotificationinterruptionEndedNotificationruntimeErrorNotification
Configuring multitasking
Monitoring performance
Configuring the app’s audio session
usesApplicationAudioSessionautomaticallyConfiguresApplicationAudioSessionconfiguresApplicationAudioSessionToMixWithOthersconfiguresApplicationAudioSessionForBluetoothHighQualityRecording