Contents

AVCaptureSession

An object that configures capture behavior and coordinates the flow of data from input devices to capture outputs.

Declaration

class AVCaptureSession

Mentioned 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

Configuring deferred start

Configuring capture controls

Managing the session life cycle

Observing session state

Configuring multitasking

Monitoring performance

Configuring the app’s audio session

Managing color spaces

Synchronizing output

See Also

Capture sessions