Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
}
like image 815
borrel Avatar asked Sep 27 '16 00:09

borrel


1 Answers

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

like image 183
borrel Avatar answered Sep 28 '22 08:09

borrel