14.3 C
London
Thursday, September 12, 2024

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


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 beneath.

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 isn't 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.accommodates(playerNode), !playerNode.isPlaying {
        print("[endcall debug] record of nodes: (audioEngine.attachedNodes)")
        audioEngine.detach(playerNode)
    } else {
        print("[endcall] node is busy")
        return
    }
}

Nonetheless, when I attempt to detach the playerNode, the appliance 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’, motive:
‘required situation is fake:
(graphNode->IsNodeState(kAUGraphNodeState_InInputChain) ||
graphNode->IsNodeState(kAUGraphNodeState_InOutputChain))’

I’m not sure why this error happens when detaching the node. Here is what I am making an attempt to know and repair:

  1. What could possibly be inflicting this error when I attempt to detach the playerNode?
  2. Is there a particular process I have to observe earlier than detaching a node from AVAudioEngine?
  3. Any ideas or steering on the right way to resolve or keep away from this error could be tremendously appreciated.
    Thanks to your assist!
Latest news
Related news

LEAVE A REPLY

Please enter your comment!
Please enter your name here