avfoundationios10avaudioconverter

AVAudioConverter is broken in iOS 10


AVAudioConverter seems broken in iOS 10. The code was working in iOS 9 and now

Error Domain=NSOSStatusErrorDomain Code=-50 "(null)"

is returned no matter what audio format is used. It suprises me every year, that basic library functionality stops working.

func audioConverterFailureIOS10() {
    // Describe the audio format
    let inFormat = AVAudioFormat(standardFormatWithSampleRate: 44100, channels: 2)
    let outFormat = AVAudioFormat(standardFormatWithSampleRate: 22050, channels: 2)

    // Allocate buffers
    let outBuffer = AVAudioPCMBuffer(pcmFormat: outFormat, frameCapacity: 1024)

    // Create an input block that is called when the converter needs input
    let inputBlock : AVAudioConverterInputBlock = { (inNumPackets, outStatus) -> AVAudioBuffer? in
        // Fails before entering here
        return nil
    }

    // Create the audio converter
    let converter = AVAudioConverter(from: inFormat, to: outFormat)

    var error : NSError?
    _ = converter.convert(to: outBuffer, error: &error, withInputFrom: inputBlock)

    // Error Domain=NSOSStatusErrorDomain Code=-50 "(null)"
    print(error)
}

Solution

  • So, it turned out that the outBuffer frameLength has to be set to the frameCapacity. As default the length is 0 and is apparently treated differently on iOS 10