我编写了以下AVAudioEngine代码,用于初始化我的一个自定义AUAudioUnit
子类,并将其连接到引擎的主混音器节点。
class AudioEngine {
let engine = AVAudioEngine()
init() {
let sess = AVAudioSession.sharedInstance()
try! sess.setCategory(AVAudioSessionCategoryPlayback)
try! sess.setActive(true)
let desc = AudioComponentDescription(componentType: kAudioUnitType_Generator, componentSubType: 0x00000001, componentManufacturer: 0x00000002, componentFlags: 0, componentFlagsMask: 0)
AUAudioUnit.registerSubclass(MyAudioUnit.self, as: desc, name: "MyAudioUnit", version: 2)
let format = engine.outputNode.outputFormat(forBus: 0)
engine.connect(engine.mainMixerNode, to: engine.outputNode, format: format)
AVAudioUnit.instantiate(with: desc, options: []) { (audiounit, error) in
guard let au = audiounit else { return }
self.engine.attach(au)
let stereoFormat = AVAudioFormat(standardFormatWithSampleRate: format.sampleRate, channels: 2)
// CRASHES HERE
self.engine.connect(au, to: self.engine.mainMixerNode, format: stereoFormat)
do {
try self.engine.start()
} catch (let exceptionError) {
print(exceptionError)
}
}
}
}
在说" CRASHES HERE"的行上,我得到一个例外并看到:
2017-07-22 14:20:39.208951-0400 AUInProcessTest [26638:7238083] [avae] AVAEInternal.h:98:_AVAE_CheckSuccessAndNoNSError:[AUInterface.mm:512:SetFormat:([[busArray objectAtIndexedSubscript:(NSUInteger) element] setFormat:format error:& nsErr])]返回false,错误(null) 2017-07-22 14:20:39.217199-0400 AUInProcessTest [26638:7238083] ***因未捕获的异常而终止应用程序' com.apple.coreaudio.avfaudio',原因:' [[ busArray objectAtIndexedSubscript:(NSUInteger)element] setFormat:format error:& nsErr]:return false,error(null)'
我不确定这意味着什么。