Format wrong while recording in swift

I have a Flutter app and want to record audio in the native Swift part. I use an audio unit for this.
I now have the problem that I cannot process the audio data in the RenderCallback correctly. When I initialize the AudioUnitRender I get the error:

1024 frames, 4 bytes/frame, expected 4096-byte buffer; ioData.mBuffers[0].mDataByteSize=2048; kAudio_ParamError
  from AU (0x10660f5e0): auou/vpio/appl, render err: -50
AudioUnitRender failed with status: -50
throwing -1
  from AU (0x10660f5e0): auou/vpio/appl, render err: -1

But that can’t actually happen, because I set the audioFormat in the init method to 2 bytes/frame. Does anyone have an idea what the problem is?

Here is the code:

startRecording function

func startRecording() {
  let audioSession = AVAudioSession.sharedInstance()
  do {
    try audioSession.setCategory(.playAndRecord, options: [.allowBluetooth])
    try audioSession.setMode(.voiceChat)
    try audioSession.setActive(true)
  } catch {
    print("Failed to set up audio session: (error.localizedDescription)")
  }
  initAudioUnit()
  AudioOutputUnitStart(ioUnitUdp!)
}

init audio unit

func initAudioUnit() {

  var status: OSStatus

  var ioUnitDescription = AudioComponentDescription(
    componentType: kAudioUnitType_Output,
    componentSubType: kAudioUnitSubType_VoiceProcessingIO,
    componentManufacturer: kAudioUnitManufacturer_Apple,
    componentFlags: 0,
    componentFlagsMask: 0
  )
  let inputComponent = AudioComponentFindNext(nil, &ioUnitDescription)
  status = AudioComponentInstanceNew(inputComponent!, &ioUnitUdp)

  var one: UInt32 = 1
  // Enable IO for recording
  status = AudioUnitSetProperty(
    ioUnitUdp!, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, kInputBus, &one,
    UInt32(MemoryLayout<UInt32>.size))

  var audioFormat = AudioStreamBasicDescription()
  memset(&audioFormat, 0, MemoryLayout<AudioStreamBasicDescription>.size)
  audioFormat.mSampleRate = 8000.0
  audioFormat.mFormatID = kAudioFormatLinearPCM
  audioFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked
  audioFormat.mFramesPerPacket = 1
  audioFormat.mChannelsPerFrame = 1
  audioFormat.mBytesPerFrame = 2
  audioFormat.mBytesPerPacket = 2
  audioFormat.mBitsPerChannel = 16

  status = AudioUnitSetProperty(
    ioUnitUdp!, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, kOutputBus, &audioFormat,
    UInt32(MemoryLayout<AudioStreamBasicDescription>.size))

  // Set input callback
  var callbackStruct1: AURenderCallbackStruct = AURenderCallbackStruct()
  callbackStruct1.inputProc = renderCallbackFunction
  callbackStruct1.inputProcRefCon = UnsafeMutableRawPointer(
    Unmanaged.passUnretained(self).toOpaque())  // self
  status = AudioUnitSetProperty(
    ioUnitUdp!, kAudioOutputUnitProperty_SetInputCallback, kAudioUnitScope_Global, kInputBus,
    &callbackStruct1, UInt32(MemoryLayout<AURenderCallbackStruct>.size))

  var bypassVP: UInt32 = 0
  status = AudioUnitSetProperty(
    ioUnitUdp!, kAUVoiceIOProperty_BypassVoiceProcessing, kAudioUnitScope_Global, kOutputBus,
    &bypassVP, UInt32(MemoryLayout<UInt32>.size))

  var vpAGC: UInt32 = 0
  status = AudioUnitSetProperty(
    ioUnitUdp!, kAUVoiceIOProperty_VoiceProcessingEnableAGC, kAudioUnitScope_Global, kOutputBus,
    &vpAGC, UInt32(MemoryLayout<UInt32>.size))

  var quality: UInt32 = 127
  status = AudioUnitSetProperty(
    ioUnitUdp!, kAUVoiceIOProperty_VoiceProcessingQuality, kAudioUnitScope_Global, kInputBus,
    &quality, UInt32(MemoryLayout<UInt32>.size))

  var unmuteOutput: UInt32 = 0
  status = AudioUnitSetProperty(
    ioUnitUdp!, kAUVoiceIOProperty_MuteOutput, kAudioUnitScope_Global, kOutputBus, &unmuteOutput,
    UInt32(MemoryLayout<UInt32>.size))

  // Initialise
  status = AudioUnitInitialize(ioUnitUdp!)
}

render callback

let renderCallbackFunction: AURenderCallback = {
  (
    inRefCon,
    ioActionFlags,
    inTimeStamp,
    inBusNumber,
    inNumberFrames,
    ioData
  ) -> OSStatus in

  let appDelegate = Unmanaged<AppDelegate>.fromOpaque(inRefCon).takeUnretainedValue()

  var bufferList = AudioBufferList(
    mNumberBuffers: 1,
    mBuffers: AudioBuffer(
      mNumberChannels: 1,
      mDataByteSize: inNumberFrames * UInt32(MemoryLayout<Int16>.size),
      mData: malloc(Int(inNumberFrames) * MemoryLayout<Int16>.size)
    )
  )

  guard let data = bufferList.mBuffers.mData else {
    return noErr
  }

  // Call AudioUnitRender to obtain recorded samples
  let status = AudioUnitRender(
    appDelegate.ioUnitUdp!, ioActionFlags, inTimeStamp, inBusNumber, inNumberFrames, &bufferList)

  if status != noErr {
    print("AudioUnitRender failed with status: (status)")
    return status
  }

  print("data: (bufferList)")
  // DO Stuff with data here

  free(data)

  return noErr
}

ioUnitUdp is a variable in appDelegate

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật