iosavfoundationavaudiopcmbuffer

Swift: AVAudioPCMBuffer vs AVAudioBuffer vs AVAudioCompressedBuffer


I'm currently using an AVAudioPCMBuffer to play a .wav file in a simple game. However, when checking the documentation for it, I came across two other types of buffers I never saw anywhere else, the ones in the title: AVAudioBuffer and AVAudioCompressedBuffer.

Which buffer should I be using, and why? Any help would be appreciated!


Solution

  • AVAudioBuffer is the superclass of AVAudioPCMBuffer and AVAudioCompressedBuffer.

    You shouldn't have to deal with AVAudioBuffer it in practice, normally you just use its subclasses.

    For your specific use case, I'd suggest using a simpler way to play the file: AVAudioFile(forReading: URL) which you can directly feed into a AVAudioPlayerNode using scheduleFile(_:, at:) which would automatically handle the buffering.