6

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)
}
borrel
  • 251
  • 5
  • 14
  • Wow. Do apps built against the 9.3 SDK work? – Rhythmic Fistman Sep 27 '16 at 01:30
  • @RhythmicFistman Exactly. I installed Xcode 7.3 and ran the exact same code (with Swift 2.x syntax) against the 9.3 SDK and **there is no error**. The "funny" thing is, that last year when iOS 9 was released, I updated my code using AVAudioConverter instead of the older Extended Audio File Services due to segmentation faults inside the Apple lib. Maybe they have fixed that and instead broke the other one... – borrel Sep 27 '16 at 07:45

1 Answers1

7

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

borrel
  • 251
  • 5
  • 14