ios - AudioUnitRender 错误 -50,缓冲区长度为奇数

标签 ios core-audio audiounit remoteio

我有一个配置了 AVAudioSessionCategoryPlayAndRecord 的 RemoteIO 单元。我发现其中有一些奇怪的行为。我打开应用程序,并在 audioUnit 完全初始化之前立即关闭它(它实际上在后台初始化,因为我过早退出应用程序)。接下来,我将应用程序置于前台并立即重新启动,我看到 AudioUnitRender 连续失败并出现错误 -50。我发现 inNumberFrames 是 1115,只要这个数字是奇数,它就会失败。

  func recordingCallback(inRefCon:UnsafeMutableRawPointer,
                ioActionFlags:UnsafeMutablePointer<AudioUnitRenderActionFlags>,
                inTimeStamp:UnsafePointer<AudioTimeStamp>,
                inBusNumber:UInt32,
                inNumberFrames:UInt32,
                ioData:UnsafeMutablePointer<AudioBufferList>?) -> OSStatus
  {
       let controller = unsafeBitCast(inRefCon, to: MicrophoneOutput.self) 

      let listPtr = controller.audioBufferList.unsafeMutablePointer

     let buffers = UnsafeBufferPointer<AudioBuffer>(start: &listPtr.pointee.mBuffers, count: Int(listPtr.pointee.mNumberBuffers))

   for var buf in buffers {
       buf.mDataByteSize = inNumberFrames * UInt32(sampleWordSize)
    }

   let status = AudioUnitRender(controller.audioUnit!, ioActionFlags, inTimeStamp, inBusNumber, inNumberFrames, listPtr)

if noErr != status {
   print("Error \(status)");
   NSLog("\(AVAudioSession.sharedInstance().currentRoute.inputs[0])")
   fatalError("Render status \(status)")
  // return status;
}

return noErr

奇怪的是,如果我在 AudioUnitRender 调用中将 inNumberFrames 硬编码为 1114,它就会成功!有谁知道发生了什么事?模式是一旦应用程序从后台恢复,就会有一个路由更改通知,然后回调中的第一个样本长度为 1114 帧,但它旁边的样本有 1115 帧失败。

最佳答案

原来是对Swift语言的误解导致的编程错误。罪魁祸首是:

for var buf in buffers {
   buf.mDataByteSize = inNumberFrames * UInt32(sampleWordSize)
}

这个答案提供了更多细节 - Swift vs Objective C pointer manipulation issue

关于ios - AudioUnitRender 错误 -50,缓冲区长度为奇数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52061575/

相关文章:

iphone - 如何录制 .mp3 格式的音频文件?

c++ - IPlugEffect(音频单元目标)“在打开“resource.h”期间出现SysError 0”

ios - 如何在 iOS 上的 AUGraph 中添加两个 I/O 音频单元?

core-audio - Audio Session "Ducking"在 iOS 4 中损坏...?

ios - 核心音频 iOS : Retrieving the past & future timestamps for captured & to-be-rendered samples

ios - Core Audio Swift Equalizer 一次调整所有频段?

ios - 可以忽略 UIImageViews 透明部分的平移手势吗?

php - 推送通知适用于开发,而不是生产

ios - 如何在UIImageView左上角和右下角创建UIButton

android - 我可以向群组中的单个 iBeacon 设备发送消息吗?