Search code examples
iosmacosaudioavfoundationcore-audio

What should I use for decoding/converting audio from file or buffer using AVFoundation or CoreAudio?


I want to decode audio from a buffer or audio file and write it to a buffer for playback (I don't want to route it directly to the hardware) on macOS and iOS. I want to be able to convert it to another format too if supported. What classes/services should I use from AVFoundation/CoreAudio API to do that?


Solution

  • AVFoundation has classes that can do all these things.

    1. AVAudioFile for decoding audio from a file to PCM (AVAudioPCMBuffer).
    2. AVAudioConverter for converting between formats
    3. AVAudioEngine with an AVAudioPlayerNode that can play back AVAudioPCMBuffers

    You might insetad use CoreAudio and AudioToolbox if you need more control over latency, devices or if you run into bugs in AVFoundation.