ios - AVAudioEngine.connect 上的 CoreAudio 错误和崩溃
问题描述
我正在尝试将一个连接AVAudioUnitEffect
到这样的实例AVAudioEngine
:
required init(inputFormat: AVAudioFormat, outputFormat: AVAudioFormat, andAVAudioEngine avAudioEngine:AVAudioEngine) {
self.inputFormat = inputFormat
self.outputFormat = outputFormat
self.avAudioEngine = avAudioEngine
self.myAudioUnit = MyAVAudioUnit()
super.init()
avAudioEngine.attach(myAudioUnit)
avAudioEngine.connect(myAudioUnit, to: avAudioEngine.outputNode, format: self.inputFormat)
}
总体类只是 的子类,NSObject
并且MyAudioUnit
是 的子类AVAudioUnitEffect
。
在看似随机的时间,这个初始化程序的最后一行(对 的调用connect
)将抛出一个带有以下错误的 SIGABRT:com.apple.coreaudio.avfaudio: error -10875
相当于kAudioUnitErr_FailedInitialization
。
任何人都可以阐明这个错误以及这里可能发生的事情吗?我认为可能是初始化程序MyAVAudioUnit
失败了,但它的内部初始化程序 ( init(audioComponentDescription: AudioComponentDescription)
) 不会抛出任何错误并且具有非可选的返回类型。有没有其他人有过这个特殊错误的经验?
更新
这里是初始化inputFormat
:
guard let stereoFormat = AVAudioFormat(commonFormat: .pcmFormatFloat32,
sampleRate: 44100,
channels: 2,
interleaved: false) else {
return
}
let numChannels = UInt32(10)
guard let multiChannelLayout = AVAudioChannelLayout(layoutTag: kAudioChannelLayoutTag_Unknown | numChannels) else {
return
}
inputFormat = AVAudioFormat(commonFormat: stereoFormat.commonFormat,
sampleRate: stereoFormat.sampleRate,
interleaved: stereoFormat.isInterleaved,
channelLayout: multiChannelLayout)
MyAVAudioUnit
包含一个额外的自定义参数 ( volumeParameter
) 并被初始化为:
required override init() {
var componentDescription = AudioComponentDescription()
componentDescription.componentType = kAudioUnitType_Effect
componentDescription.componentSubType = xxxxx
componentDescription.componentManufacturer = xxxxx
componentDescription.componentFlags = 0
componentDescription.componentFlagsMask = 0
AUAudioUnit.registerSubclass(MyAVAudioUnit.self,
as: componentDescription,
name:"MyAVAudioUnit",
version: UInt32.max)
super.init(audioComponentDescription: componentDescription)
guard let paramTree = self.auAudioUnit.parameterTree else { return }
volumeParameter = paramTree.value(forKey: "volumeParameter") as? AUParameter
}