AVAudioEngine detachNode 在 Swift 中崩溃并出现“所需条件为 false”错误

AVAudioEngine detachNode Crashes with 'required condition is false' Error in Swift

提问人:eugene_prg 提问时间:11/17/2023 更新时间:11/17/2023 访问量:15

问:

我正在使用 Swift 开发音频处理应用程序,并在尝试分离 AVAudioPlayerNode 时遇到 AVAudioEngine 问题。我的应用程序是一个播放音频文件的呼叫模拟器。

我在下面分享了相关的代码片段。

用于附加和播放节点的代码:

let audioFile = try AVAudioFile(forReading: fileURL)
let audioFormat = audioFile.processingFormat
let audioFrameCount = UInt32(audioFile.length)
let audioFileBuffer = AVAudioPCMBuffer(pcmFormat: audioFormat, frameCapacity: audioFrameCount)!
try audioFile.read(into: audioFileBuffer)

if playerNode == nil {
    playerNode = AVAudioPlayerNode()
    audioEngine.attach(playerNode!)

    // Use the stored outputNode for connection
    if let outputNode = self.outputNode {
        audioEngine.connect(playerNode!, to: outputNode, format: audioFormat)
    } else {
        print("Output node is not initialized")
        return
    }
}

// Start the engine if it's not running
if !audioEngine.isRunning {
    try audioEngine.start()
}

// Schedule the buffer and play
playerNode?.scheduleBuffer(audioFileBuffer, at: nil, options: []/* .loops*/, completionHandler: nil)
playerNode?.play()

结束调用和分离节点的代码:

if let playerNode = playerNode, audioEngine.isRunning {
    playerNode.stop()
    
    if audioEngine.attachedNodes.contains(playerNode), !playerNode.isPlaying {
        print("[endcall debug] list of nodes: \(audioEngine.attachedNodes)")
        audioEngine.detach(playerNode)
    } else {
        print("[endcall] node is busy")
        return
    }
}

但是,当我尝试分离 playerNode 时,应用程序崩溃并出现以下错误:

2023-11-17 14:20:17.372029+0100 CallSimulator[99266:3421546] [avae] AVAEInternal.h:76 必需条件为 false: [AVAudioEngineGraph.mm:1771:删除节点: ((graphNode->IsNodeState(kAUGraphNodeState_InInputChain) || graphNode->IsNodeState(kAUGraphNodeState_InOutputChain)))] 2023-11-17 14:20:17.418217+0100 CallSimulator[99266:3421546] *** 终止应用程序 由于未捕获异常“com.apple.coreaudio.avfaudio”,原因: '必需条件为 false: (graphNode->IsNodeState(kAUGraphNodeState_InInputChain) || graphNode->IsNodeState(kAUGraphNodeState_InOutputChain))'

我不确定为什么在分离节点时会出现此错误。这是我试图理解和修复的内容:

  1. 当我尝试分离 playerNode 时,是什么原因导致了此错误?
  2. 在将节点与 AVAudioEngine 分离之前,是否需要遵循特定过程?
  3. 有关如何解决或避免此错误的任何建议或指导将不胜感激。 感谢您的帮助!
iOS Swift 音频 AVAudioEngine

评论


答: 暂无答案