ios – AVAudioEngine detachNode Crashes with ‘required situation is fake’ Error in Swift

Spread the love


I am engaged on an audio processing software utilizing Swift and encountering a problem with AVAudioEngine when I attempt to detach an AVAudioPlayerNode. My software is a name simulator that performs audio recordsdata.

I’ve shared the related code snippets under.

Code for attaching and taking part in the node:

let audioFile = attempt AVAudioFile(forReading: fileURL)
let audioFormat = audioFile.processingFormat
let audioFrameCount = UInt32(audioFile.size)
let audioFileBuffer = AVAudioPCMBuffer(pcmFormat: audioFormat, frameCapacity: audioFrameCount)!
attempt audioFile.learn(into: audioFileBuffer)

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

    // Use the saved outputNode for connection
    if let outputNode = self.outputNode {
        audioEngine.join(playerNode!, to: outputNode, format: audioFormat)
    } else {
        print("Output node shouldn't be initialized")
        return
    }
}

// Begin the engine if it isn't working
if !audioEngine.isRunning {
    attempt audioEngine.begin()
}

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

Code for ending the decision and detaching the node:

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

Nonetheless, when I attempt to detach the playerNode, the applying crashes with the next error:

2023-11-17 14:20:17.372029+0100 CallSimulator[99266:3421546] [avae]
AVAEInternal.h:76 required situation is fake:
[AVAudioEngineGraph.mm:1771:RemoveNode:
((graphNode->IsNodeState(kAUGraphNodeState_InInputChain) ||
graphNode->IsNodeState(kAUGraphNodeState_InOutputChain)))] 2023-11-17
14:20:17.418217+0100 CallSimulator[99266:3421546] *** Terminating app
resulting from uncaught exception ‘com.apple.coreaudio.avfaudio’, cause:
‘required situation is fake:
(graphNode->IsNodeState(kAUGraphNodeState_InInputChain) ||
graphNode->IsNodeState(kAUGraphNodeState_InOutputChain))’

I’m uncertain why this error happens when detaching the node. This is what I am making an attempt to grasp and repair:

  1. What might be inflicting this error when I attempt to detach the playerNode?
  2. Is there a particular process I must comply with earlier than detaching a node from AVAudioEngine?
  3. Any strategies or steering on learn how to resolve or keep away from this error can be vastly appreciated.
    Thanks in your assist!

Leave a Reply

Your email address will not be published. Required fields are marked *