我试图在Swift中使用新的AUAudioUnit API在iOS和macOS上使用VoiceProcessingIO单元,如下所示:
var description = AudioComponentDescription()
description.componentType = kAudioUnitType_Output
description.componentSubType = kAudioUnitSubType_VoiceProcessingIO
description.componentManufacturer = kAudioUnitManufacturer_Apple
description.componentFlags = 0
description.componentFlagsMask = 0
var vpio: AUAudioUnit
vpio = try! AUAudioUnit(componentDescription: description)
print("canPerformInput:", vpio.canPerformInput)
print("isInputEnabled:", vpio.isInputEnabled)
print("canPerformOutput:", vpio.canPerformOutput)
print("isOutputEnabled:", vpio.isOutputEnabled)
print("---")
vpio.isInputEnabled = true
vpio.inputHandler = { (actionFlags, timestamp, frameCount, inputBusNumber) in
print("inputHandler called")
}
vpio.outputProvider = { (actionFlags, timestamp, frameCount, inputBusNumber, inputData) -> AUAudioUnitStatus in
print("outputProvider called")
return noErr
}
try! vpio.allocateRenderResources()
try! vpio.startHardware()
此代码适用于iOS(10.2),它调用inputHandler和outputProvider:
canPerformInput: true
isInputEnabled: false
canPerformOutput: true
isOutputEnabled: true
---
inputHandler called
outputProvider called
inputHandler called
outputProvider called
...
在macOS(10.12.2)上,只调用outputProvider:
canPerformInput: true
isInputEnabled: true
canPerformOutput: true
isOutputEnabled: true
---
outputProvider called
outputProvider called
outputProvider called
outputProvider called
...
观察:在iOS上,isInputEnabled默认设置为false,必须启用(第17行)才能调用inputHandler。在macOS上,默认情况下启用它,但不调用inputHandler。像上面那样明确设置isInputEnabled也不起作用。
当使用Apple's echoTouch sample code和suggested here by theanalogkid中使用的旧API(AudioUnit,AudioComponentInstanceNew,AudioUnitSetProperty,kAudioOutputUnitProperty_SetInputCallback等...)时,VPIO可以在macOS上正常工作(除非在macOS上你不能和不要&# 39; t需要设置kAudioOutputUnitProperty_EnableIO)
我在这里做错了什么,或者这可能是苹果公司的一个错误?