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)
    }

Accepted Reply

I think you're forgetting to set the outBuffer frameLength to the frameCapacity.


https://forums.developer.apple.com/thread/24124

Replies

I think you're forgetting to set the outBuffer frameLength to the frameCapacity.


https://forums.developer.apple.com/thread/24124

Thanks a lot, that did the trick! frameLength is 0 by default and is apparently treated differently on iOS 10.

What else could be wrong? I still get the -50, even if I try to convert to the very same format. Moreover, no matter which combination of input and output formats I supply, AVAudioConverter always reports nil to availableEncodeSampleRates, availableEncodeBitRates, and availableChannelLayoutTags. iOS 10.2 here.