我有一个配置了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调用中将NumberFrames硬编码为1114,它将成功!有人知道发生了什么吗?该模式是在应用程序从后台恢复后立即发出路由更改通知,然后回调中的第一个示例的长度为1114帧,但其旁边的示例为1115帧,失败了。
答案 0 :(得分:1)
原来是由于对Swift语言的误解造成的编程错误。罪魁祸首是:
for var buf in buffers {
buf.mDataByteSize = inNumberFrames * UInt32(sampleWordSize)
}
此答案提供了更多详细信息- Swift vs Objective C pointer manipulation issue