skip to content

In AVFoundation, how do you configure an AVCaptureSession to film a dance routine with camera and microphone, and where should startRunning() be called?

level: middleimportance: must knowfreq 62%

answer

  1. inputs feed outputs through one session
  2. two Info.plist usage strings
  3. ask before you add devices
  4. batch changes, then commit
  5. a call that blocks until started

basics

~10 s

Check camera and microphone authorization, then between beginConfiguration() and commitConfiguration() add an AVCaptureDeviceInput per device and an AVCaptureMovieFileOutput, checking canAddInput and canAddOutput. Call startRunning() on a serial background queue, because it blocks.

solid answer

~30 s

First permission: the app needs `NSCameraUsageDescription` and `NSMicrophoneUsageDescription` in Info.plist, then `AVCaptureDevice.authorizationStatus(for:)` for `.video` and `.audio`, calling `requestAccess(for:)` when the status is `.notDetermined`. Then I configure on a private serial queue: `beginConfiguration()`, an `AVCaptureDeviceInput` for the back wide-angle camera and one for the microphone, each guarded by `canAddInput(_:)`, an `AVCaptureMovieFileOutput` guarded by `canAddOutput(_:)`, a `sessionPreset`, and `commitConfiguration()`. Finally `startRunning()` on that same queue, because Apple documents it as a blocking call that returns only when the session has started or failed. Recording is then `startRecording(to:recordingDelegate:)`, and the file is usable only once the delegate's `fileOutput(_:didFinishRecordingTo:from:error:)` arrives.

code

swift · 13 lines
swift
import AVFoundation

// Camera and microphone each need their own grant (and Info.plist usage string).
func isCaptureAuthorized(for media: AVMediaType) async -> Bool {
    switch AVCaptureDevice.authorizationStatus(for: media) {
    case .authorized:
        return true
    case .notDetermined:
        return await AVCaptureDevice.requestAccess(for: media)
    default:
        return false // .denied or .restricted: send the user to Settings
    }
}

go deeper

for a junior

Recall the parts: a session, a device input per device, a movie file output, and the two Info.plist usage strings.

for a middle

Explain begin and commit configuration, the canAdd checks and their exceptions, and why startRunning() runs on a serial session queue.

for a senior

Show you can diagnose a frozen camera screen, a crash on addInput, a silent recording and a half-written file, each from its documented cause.

for a principal

Set the team rule that every session call goes through one serial queue, and decide which camera features justify a custom session at all.

## The capture model `AVCaptureSession` is the hub of every camera or microphone feature in AVFoundation. It manages the app's exclusive access to the capture hardware and the flow of media from **inputs** to **outputs**: - An **input** is an `AVCaptureInput` subclass; for hardware it is an `AVCaptureDeviceInput` wrapping an `AVCaptureDevice`, such as the back `.builtInWideAngleCamera` or the default audio device. - An **output** is an `AVCaptureOutput` subclass that turns that media into something: `AVCaptureMovieFileOutput` writes a movie file, `AVCapturePhotoOutput` (iOS 10) takes photos, `AVCaptureVideoDataOutput` hands you raw frames. - A **preview** is an `AVCaptureVideoPreviewLayer`, a Core Animation layer whose `session` you set; a `UIView` subclass that returns it from `layerClass` is the usual host. To film a routine with sound you need two inputs, camera and microphone, and one movie output. ## Permission comes before configuration Apple's authorization article sets two rules. The Info.plist must carry `NSCameraUsageDescription` and `NSMicrophoneUsageDescription`; without the key, **the system terminates the app** when it requests authorization or uses the device. And the app should test `AVCaptureDevice.authorizationStatus(for:)` before setting up a session: | Status | Meaning | What the app does | |---|---|---| | `.notDetermined` | never asked | call `requestAccess(for:)` at the moment the user opens the camera | | `.authorized` | granted | configure the session | | `.denied`, `.restricted` | refused or blocked | explain and link to Settings | Camera and microphone are separate grants, so a routine with sound checks both. Creating an `AVCaptureDeviceInput` while the status is `.notDetermined` triggers the prompt on your behalf, but asking explicitly keeps the moment under your control. ## Configuring atomically `beginConfiguration()` and `commitConfiguration()` batch changes: everything between them, adding or removing inputs and outputs, changing `sessionPreset`, configuring a connection, is pended and applied together at the outermost commit. Pairs may nest. The header adds two hard rules: 1. If you call `startRunning()` or `stopRunning()` while a `beginConfiguration()` is still open, AVFoundation throws an **`NSGenericException`**. 2. Calling `addInput(_:)` or `addOutput(_:)` without a prior `true` from `canAddInput(_:)` or `canAddOutput(_:)` throws an **`NSInvalidArgumentException`**. Both can be called on a running session, which is how an app switches cameras: begin, remove the old input, add the new one, commit. Quality is a session setting. `sessionPreset` takes values such as `.high`, `.photo` or `.hd1920x1080`, and `canSetSessionPreset(_:)` checks that the current inputs support one before you set it. The class documentation notes that specialised modes, high frame rate for a slow-motion replay of a spin for example, need a capture format set directly on the `AVCaptureDevice` rather than a preset. ## Starting without freezing the UI The header for `startRunning()` says it **blocks until the session has completely started up or failed**, and the class documentation says to start the session on a **serial dispatch queue** so the main queue stays responsive. `stopRunning()` blocks the same way. The usual design keeps one private serial queue for every session call: configuration, start, stop and camera switches, and Apple points to its AVCam sample for an implementation. That serialises access to the session and keeps a slow start away from the main thread. A failure to start is reported through `AVCaptureSession.runtimeErrorNotification`. Interruptions are separate: moving the app to the background posts `wasInterruptedNotification` with the reason `videoDeviceNotAvailableInBackground`, and if you have not called `stopRunning()` the session resumes when the app returns and posts `interruptionEndedNotification`. ## Recording the routine With the session running, `movieOutput.startRecording(to:recordingDelegate:)` begins writing. `stopRecording()` ends it, but the remaining data is written in the background, so the file is ready only when the delegate receives `fileOutput(_:didFinishRecordingTo:from:error:)`. `maxRecordedDuration` and `maxRecordedFileSize` stop recording at a limit and report it through that same callback. When a feature needs the frames themselves, say to analyse a dancer's pose live, the output is `AVCaptureVideoDataOutput`. Its `setSampleBufferDelegate(_:queue:)` takes a serial queue so frames arrive in order, and `alwaysDiscardsLateVideoFrames` (default `true`) drops frames that arrive while your delegate is still busy. Filtering those frames is the subject of the Core Image lesson; augmented-reality camera work belongs to ARKit. ## Mistakes worth catching in review - Calling `startRunning()` from `viewDidLoad` on the main thread, which stalls the screen while the camera warms up. - Adding inputs without `canAddInput(_:)`, which crashes on a device or preset that does not support the combination. - Forgetting the microphone input, then wondering why the routine has no sound. - Reading the movie file right after `stopRecording()` instead of in the delegate callback. - Omitting a usage string, which ends the app instead of showing an alert.

  • How do you switch from the back to the front camera while recording is previewed?
    On the session queue, call `beginConfiguration()`, remove the current camera's `AVCaptureDeviceInput`, create one for the front device, add it if `canAddInput(_:)` returns `true` (otherwise re-add the old one), then `commitConfiguration()`. The session keeps running, and the change applies as one atomic update at the commit.
  • What happens to the session when the instructor switches to another app mid-take?
    Camera use is prohibited in the background, so the session posts `wasInterruptedNotification` with the reason `videoDeviceNotAvailableInBackground`. If the app has not called `stopRunning()`, its start request is preserved: on return the session posts `interruptionEndedNotification` and runs again. The app should update its record button from those notifications.
  • When is the recorded movie file safe to read?
    Only after the recording delegate receives `fileOutput(_:didFinishRecordingTo:from:error:)`. `stopRecording()` returns before the remaining data has been written in the background, so reading or uploading the file straight after it can find an incomplete movie.

  • startRunning() returns at once, so calling it on the main thread is fine
  • One camera permission also covers the microphone
  • addInput(_:) quietly ignores an input the session cannot take
  • The movie file is complete as soon as stopRecording() returns
  • A missing camera usage string only shows a generic permission alert