Mobile Development 7 min read

Implementing Audio Capture and Data Multiplexing in iOS Applications Using Swift

This technical guide demonstrates how to configure an iOS audio capture session using Swift, implement a custom multiplexer class to safely route microphone input to multiple processing destinations, and efficiently handle real-time audio data streams for language learning applications.

Liulishuo Tech Team
Liulishuo Tech Team
Liulishuo Tech Team
Implementing Audio Capture and Data Multiplexing in iOS Applications Using Swift

This article details the implementation of real-time audio capture and routing within an iOS application designed for language learning and exam preparation. The primary objective is to record student responses while simultaneously streaming audio to a feedback recognition service via WebSocket, ensuring the entire process completes in under a second for optimal user experience.

To achieve this, developers utilize the AVCaptureSession class to interface with the device's built-in microphone. The session is configured by adding a microphone input and preparing outputs for both local file storage and real-time data streaming. However, attempting to attach multiple AVCaptureAudioDataOutput instances directly to the capture session will cause a runtime crash.

To resolve this limitation, the author introduces a custom AudioDataOutputMultiplexer class that acts as the sole delegate for the capture session. This multiplexer safely distributes incoming audio sample buffers to multiple downstream consumers. The implementation is structured as follows:

self.captureSession = AVCaptureSession()
captureSession.beginConfiguration()
let microphoneDevice = AVCaptureDevice.defaultDevice(withDeviceType: .builtInMicrophone, mediaType: AVMediaTypeAudio, position: .unspecified)
let input = try AVCaptureDeviceInput(device: microphoneDevice)
if captureSession.canAddInput(input) {
    captureSession.addInput(input)
}
if captureSession.canAddOutput(dataOutput) {
    captureSession.addOutput(dataOutput)
}
captureSession.commitConfiguration()

The multiplexer class conforms to AVCaptureAudioDataOutputSampleBufferDelegate and maintains an array of output targets. It includes methods to start and stop recording, effectively acting as a control switch for data flow. When the system delivers a new sample buffer, the multiplexer forwards it to all registered outputs that implement the required delegate protocol.

class AudioDataOutputMultiplexer: NSObject, AVCaptureAudioDataOutputSampleBufferDelegate {
    public let captureOutput: AVCaptureAudioDataOutput
    private var outputs: [Any]
    init() {
        self.captureOutput = AVCaptureAudioDataOutput()
        self.outputs = []
        super.init()
        let captureQueue = //...DispatchQueue
        self.captureOutput.setSampleBufferDelegate(self, queue: captureQueue)
    }
}

The core forwarding logic leverages Swift's functional programming features to safely cast and iterate over registered outputs. By using flatMap , the implementation filters out non-conforming objects and invokes the delegate method only on valid targets, gracefully handling the optional nature of the underlying Objective-C protocol.

func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!) {
    outputs
        .flatMap { $0 as? AVCaptureAudioDataOutputSampleBufferDelegate }
        .forEach { $0.captureOutput?(captureOutput, didOutputSampleBuffer: sampleBuffer, from: connection) }
}

This architecture ensures robust, crash-free audio routing, enabling simultaneous local recording and real-time server analysis. The approach is production-ready and has been successfully deployed in the IELTS practice application available on the iOS App Store.

software architectureReal-time ProcessingiOS developmentmobile app developmentAVFoundationAudio CaptureSwift Programming
Liulishuo Tech Team
Written by

Liulishuo Tech Team

Help everyone become a global citizen!

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.