我正在开发一个使用 AVFoundation 框架生成声音的应用程序。现在我想添加 Chirp.io SDK 来将一些信息编码为声音,作为我代码的补充。
如果我单独使用我的声音代码或 Chirp sdk,一切正常。当我用我的代码发出声音然后运行 Chirp 时,它只工作一次,然后在第二次尝试时失败并出现错误:
2018-10-23 19:32:55.188460+0300 FieldApp[2243:1408580] [avae] AVAEInternal.h:70:_AVAE_Check: required condition is false: [AVAudioPlayerNode.mm:536:StartImpl: (_engine->IsRunning())]
2018-10-23 19:32:55.191931+0300 FieldApp[2243:1408580] *** Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: _engine->IsRunning()'
是否可以结合使用 AVAudioEngine 和 Chirp.io SDK?
这是我的代码中的一个示例:
// ... init part of my sound code ....
let engine: AVAudioEngine = AVAudioEngine()
let playerNode: AVAudioPlayerNode = AVAudioPlayerNode()
let audioFormat = AVAudioFormat(standardFormatWithSampleRate: 44100.0, channels: 2)
engine.attach(playerNode)
engine.connect(playerNode, to: engine.mainMixerNode, format: audioFormat)
// ... Chirp init code ...
let connect: ChirpConnect! = ChirpConnect(appKey: "xXx", andSecret: "xXx")
connect.setConfig("xXxxx")
connect.start()
// ... do this in a loop ....
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, mode: AVAudioSessionModeDefault)
try AVAudioSession.sharedInstance().setActive(true)
try engine.start()
started = true
}
catch {
LogManager.shared.post("Error starting sound engine: \(error.localizedDescription)")
}
playerNode.play()
//.... wait until playing ends ....
playerNode.stop()
engine.stop()
//.... Chirp SDK sounds ....
let buf: Data = ..... some data to send
connect.send(buf)
//... end of pseudo-loop
所以当我第二次在循环中运行代码时,我得到了异常,但在engine.start()中没有得到异常,它正常执行......