AVFoundation for Swift: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction to AVFoundation

\n\n

AVFoundation is a powerful framework from Apple designed for working with audio and video on iOS, macOS, tvOS, and watchOS platforms. It provides developers with a high-level API for capturing, processing, playing back, and editing multimedia content. Unlike lower-level solutions (such as Core Audio or Video Toolbox), AVFoundation abstracts the complex details of codecs, containers, and synchronization, allowing you to focus on application logic.

\n\n

Why do you need AVFoundation? It enables solving a wide range of tasks:

\n
    \n
  • Playback: Playing audio files (MP3, AAC, WAV) and video files (MP4, MOV, M4V) with support for subtitles and chapters.
  • \n
  • Recording: Capturing video from the camera and audio from the microphone in real time.
  • \n
  • Editing: Creating compositions from multiple tracks, applying effects, trimming, changing speed.
  • \n
  • Processing: Extracting metadata, working with audio samples, applying filters.
  • \n
  • Streaming: Playing HLS (HTTP Live Streaming) content.
  • \n
\n\n

AVFoundation is the de facto standard for Apple development. If you are writing an application that needs to play music, record video, or edit podcasts — you will be using this framework.

\n\n

Installation (Adding to Project)

\n\n

AVFoundation is not installed via pip or npm, as it is a native Apple framework. It is built into Xcode and the SDK. To use it, simply import the module into your Swift files.

\n\n

Steps to add:

\n
    \n
  1. Open your project in Xcode.
  2. \n
  3. In any Swift file where you need to work with multimedia, add the import statement:
  4. \n
\n\n
import AVFoundation
\n\n

No additional actions are required. The framework is automatically available for all iOS/macOS projects.

\n\n

Configuring Permissions (iOS)

\n

To work with the camera and microphone, you need to add entries to the Info.plist file:

\n
    \n
  • NSCameraUsageDescription — description of why the app needs the camera.
  • \n
  • NSMicrophoneUsageDescription — description of why the app needs the microphone.
  • \n
\n

Without these keys, the app will crash with an error when attempting to access the devices.

\n\n

Quick Start: Playing Audio

\n\n

The simplest example is playing a local audio file. Create an instance of AVAudioPlayer and call the play() method.

\n\n
import AVFoundation\nimport UIKit\n\nclass ViewController: UIViewController {\n    var audioPlayer: AVAudioPlayer?\n\n    override func viewDidLoad() {\n        super.viewDidLoad()\n        \n        // 1. Find the file path in the Bundle\n        guard let path = Bundle.main.path(forResource: "sample", ofType: "mp3") else {\n            print("File not found")\n            return\n        }\n        \n        let url = URL(fileURLWithPath: path)\n        \n        do {\n            // 2. Initialize the player\n            audioPlayer = try AVAudioPlayer(contentsOf: url)\n            // 3. Prepare for playback (optional but recommended)\n            audioPlayer?.prepareToPlay()\n            // 4. Start playing\n            audioPlayer?.play()\n            print("Playback started")\n        } catch {\n            print("Player initialization error: \\(error.localizedDescription)")\n        }\n    }\n}
\n\n

This code creates a player, loads the sample.mp3 file from the app's resources, and starts playing it.

\n\n

Key Classes and Methods

\n\n

Below are the key AVFoundation classes and methods used in 90% of tasks. For each method, a description, parameters, and a working example are provided.

\n\n

1. AVAudioPlayer

\n

What

Recommendations