Skip to content
tezvyn:
Menu

AVFoundation: Apple's Toolkit for Audio and Video

Source: developer.apple.comEasyHow cards are made

AVFoundation: Apple's Toolkit for Audio and Video

AVFoundation is Apple's low-level toolbox for audio and video. Use it for custom camera UIs, video editing, or complex audio playback. The main footgun is its complexity; managing sessions, inputs, and outputs on the correct threads is notoriously difficult.

Why it exists

Apple needed a powerful, unified framework for developers to work with time-based audiovisual media. Higher-level APIs like UIImagePickerController are too simple for custom needs like building a unique camera interface or performing non-trivial video editing, so AVFoundation provides the necessary granular control.

The mental model

Think of AVFoundation as a professional film crew in a box. You are the director. You manage an AVCaptureSession (the production), which coordinates AVCaptureDevices (your actors and equipment, like cameras and microphones), AVCaptureInputs (what the devices capture), and AVCaptureOutputs (where the final product goes, like a file or a live preview screen).

How it works

The core of media capture is the AVCaptureSession. You configure it by adding inputs and outputs. For example, to build a camera, you find an AVCaptureDevice (the phone's back camera), create an AVCaptureDeviceInput with it, and add it to the session. Then you create an AVCaptureOutput, like a AVCaptureMovieFileOutput for saving a video, and add that too. Calling startRunning() on the session begins the flow of data from input to output. Playback and editing use different components like AVPlayer and AVComposition.

When to use it

Use AVFoundation when you need deep control over media. This includes building a custom camera UI, recording video with specific resolutions or frame rates, trimming or merging video clips programmatically, playing multiple audio tracks simultaneously, or analyzing video frames in real-time for machine learning tasks.

When not to use it

Avoid AVFoundation for simple tasks. If you just need to let the user pick a photo from their library or record a standard video, use the higher-level UIImagePickerController or the modern PhotosPicker. These APIs are far simpler and less prone to configuration errors.

One canonical example

A common use case is building a custom camera view. A developer would create an AVCaptureSession, add inputs for the back camera and microphone, and then add an AVCaptureVideoPreviewLayer (a type of output) to their view hierarchy. This displays the live camera feed. To enable recording, they would add an AVCaptureMovieFileOutput and trigger it to start and stop saving to a file based on user interaction.

Interview question

For which scenario would an iOS developer most appropriately choose AVFoundation?

  • a.To build a custom camera interface with specific resolution and frame rate controls.Correct
  • b.To play a single audio file in a simple, straightforward manner.
  • c.To allow users to select multiple images from their photo library.
  • d.To implement a standard video recording feature with the default system UI.
Why?

AVFoundation is designed for deep control over media, such as building custom camera UIs with specific recording parameters. Options A and B describe simple tasks best handled by higher-level APIs like PhotosPicker or UIImagePickerController. Option B, while technically possible, is an overkill for simple audio playback, which can be achieved with less complex APIs.

Just read this? Test yourself on what you have been reading.

Read the original → developer.apple.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on ios — each one lists the topics its interview covers.

See open roles