AVFoundation Audio Methods - Detailed Reference

Introduction

This section provides a **concise but informative overview** of the common **methods in AVFoundation** related to low-level audio tasks (using AVAudioEngine and related classes).

Each method includes a description, explanation of its ..//main.css, and a usage example to help you deeply understand how they work.

Important AVFoundation Methods for Audio

AVAudioEngine Methods

attach(_ node: AVAudioNode)

Description: Adds an audio node (like a player, mixer, or effect) to the audio engine's graph.

..//main.css:

Example:


let player = AVAudioPlayerNode()
engine.attach(player)

connect(_:to:format:)

Description: Connects the output of one node to the input of another node.

..//main.css:

  • node1: The source node.
  • node2: The destination node.
  • format: An optional AVAudioFormat describing the format of the audio stream (can be nil for automatic matching).

Example:


engine.connect(player, to: engine.outputNode, format: nil)

start()

Description: Starts the audio engine so it can begin processing audio.

Throws: An error if the engine cannot start (e.g., audio session issues).

Example:


try engine.start()

stop()

Description: Stops the engine's audio processing immediately.

Example:


engine.stop()

AVAudioPlayerNode Methods

scheduleFile(_:at:completionHandler:)

Description: Schedules an audio file to play at a specified time or immediately.

..//main.css:

  • file: The AVAudioFile to play.
  • at: An optional AVAudioTime indicating when playback should start (nil = immediate).
  • completionHandler: An optional closure called when playback finishes.

Example:


player.scheduleFile(audioFile, at: nil, completionHandler: nil)

play()

Description: Begins playback of whatever has been scheduled in the player node.

Example:


player.play()

stop()

Description: Stops playback immediately and clears scheduled data.

Example:


player.stop()

scheduleBuffer(_:at:options:completionHandler:)

Description: Schedules an in-memory audio buffer for playback.

..//main.css:

  • buffer: The AVAudioPCMBuffer to be played.
  • at: Optional AVAudioTime to specify when to start (nil = immediately).
  • options: Playback options (e.g., .loops for continuous play).
  • completionHandler: Closure called after the buffer finishes playing.

Example:


player.scheduleBuffer(buffer, at: nil, options: .loops, completionHandler: nil)

AVAudioInputNode and AVAudioOutputNode Methods

installTap(onBus:bufferSize:format:block:)

Description: Adds a "tap" to the audio stream that lets you capture audio buffers in real-time.

..//main.css:

  • onBus: The bus to install the tap on (usually 0 for default).
  • bufferSize: Number of audio frames in each captured buffer (e.g., 4096).
  • format: Audio format for the tap (can match input format).
  • block: Closure that processes each captured buffer.

Example:


inputNode.installTap(onBus: 0, bufferSize: 4096, format: format) { (buffer, time) in
    // process buffer
}

removeTap(onBus:)

Description: Removes a previously installed tap on a node.

..//main.css:

  • onBus: The bus number where the tap was installed.

Example:


inputNode.removeTap(onBus: 0)

AVAudioFile Methods

init(forReading:commonFormat:interleaved:)

Description: Creates an AVAudioFile for reading an audio file from a URL.

..//main.css:

  • forReading: URL of the audio file.
  • commonFormat: Audio format to use internally (optional).
  • interleaved: Whether to interleave the audio data (optional).

Example:


let audioFile = try AVAudioFile(forReading: fileURL)

init(forWriting:settings:commonFormat:interleaved:)

Description: Creates an AVAudioFile for writing audio data to a new file.

..//main.css:

  • forWriting: Destination URL for the audio file.
  • settings: Audio settings dictionary (e.g., sample rate, bit depth).
  • commonFormat: Common format used internally (optional).
  • interleaved: Whether to interleave the data (optional).

Example:


let outputFile = try AVAudioFile(forWriting: fileURL, settings: format.settings)

write(from:)

Description: Writes a buffer of audio data into the file.

..//main.css:

  • from: The AVAudioPCMBuffer containing audio samples.

Example:


try outputFile.write(from: buffer)

Conclusion

Mastering these fundamental methods will enable you to design powerful, professional audio apps using AVFoundation. Whether you need to build a recorder, a mixer, a synthesizer, or a live audio effects processor — understanding how these methods work is key.