AvAudioEngine 中断后未重新启动

问题描述 投票:0回答:1

我正在从麦克风获取音频。使用 AvAudioEngine 但当来电并中断时,我想停止然后重新启动获取音频进程,但当我尝试 handleInterruption(我在下面共享)方法时,我收到此错误。中断后如何停止并重新启动AvAudioEngine

AURemoteIO.cpp:1702 AUIOClient_StartIO 失败 (561145187) AVAEInternal.h:109 [AVAudioEngineGraph.mm:1545:Start: (err = PerformCommand(*ioNode, kAUStartIO, NULL, 0)): 错误 561145187 无法启动引擎:Error Domain=com.apple.coreaudio.avfaudio Code=561145187 "(null)" UserInfo={failed call=err = PerformCommand(*ioNode, kAUStartIO, NULL, 0)}

@objc func handleInterruption(notification: Notification) {
    
    guard let userInfo = notification.userInfo,
        let typeValue = userInfo[AVAudioSessionInterruptionTypeKey] as? UInt,
        let type = AVAudioSession.InterruptionType(rawValue: typeValue) else {
            return
    }
    
    // Switch over the interruption type.
    switch type {
    case .began:
        print("interrupt begin")
        socket.close()
        socket=nil
        self.socket = GCDAsyncUdpSocket(delegate: self, delegateQueue: DispatchQueue.main)
        audioEngine.stop()
        print("Audio player stopped")
   
    case .ended:
        print("interrupt end")
        
        self.audioEngine = AVAudioEngine()
        self.audioPlayer = AVAudioPlayerNode()
        self.mixer = AVAudioMixerNode()

        do {
            try AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .voiceChat, options: [.allowBluetooth, .allowBluetoothA2DP])
            
            print("Audio session category set to playback")
        } catch {
            print("Setting category to AVAudioSessionCategoryPlayback failed: \(error)")
        }

        self.mixer = AVAudioMixerNode()
        self.mixer.volume = 0
        self.audioEngine.attach(audioPlayer)
        self.audioEngine.attach(mixer)
        
        try! self.audioEngine.inputNode.setVoiceProcessingEnabled(true)
        try! AVAudioSession.sharedInstance().setActive(true)

        
        DispatchQueue.global(qos: .background).async { [weak self] in
            guard let self = self else { return }
            do {
                self.socket.setIPv4Enabled(true)
                self.socket.setIPv6Enabled(false)
                try self.socket.connect(toHost:"239.10.10.100" ?? "", onPort: 4545 ?? 0)
                try self.socket.beginReceiving()
                print("Socket started")
            } catch {
                print("Socket Started Error: \(error)")
            }
        }
        
        
        audioEngine.inputNode.installTap(onBus: 0, bufferSize: 1024, format: audioEngine.inputNode.inputFormat(forBus: 0)) {
            (buffer: AVAudioPCMBuffer!, time: AVAudioTime!) -> Void in
            do {
                let inputBlock: AVAudioConverterInputBlock = { _, outStatus in
                    outStatus.pointee = AVAudioConverterInputStatus.haveData
                    return buffer
                }
                let frameCapacity =
                AVAudioFrameCount(self.outputFormat.sampleRate) * buffer.frameLength
                / AVAudioFrameCount(buffer.format.sampleRate)
                let outputBuffer = AVAudioPCMBuffer(
                    pcmFormat: self.outputFormat,
                    frameCapacity: frameCapacity
                )!
                var error: NSError?
                self.converter.convert(to: outputBuffer, error: &error, withInputFrom: inputBlock)
                
                //let data = Data(bytes: (outputBuffer.int16ChannelData![0]), count: Int(outputBuffer.frameLength))
                let data = Data(buffer: UnsafeBufferPointer(start: outputBuffer.int16ChannelData![0], count: Int(outputBuffer.frameLength)))
                
                print(data)
                
                DispatchQueue.global(qos: .background).async { [weak self] in
                    guard let self = self else { return }
                    do {
                        self.socket.send(data, withTimeout: 0, tag: 0)
                        
                    } catch {
                        print("Socket send Error: \(error)")
                    }
                }
                
                
            } catch {
                print(error)
            }
        }
        
        audioEngine.prepare()
        do {
            try audioEngine.start()
            print("Audio player started")
        } catch {
            print("Can't start the engine: \(error)")
        }

        
    default:
        print("Default print")
        break
    }
}
swift avaudioplayer avaudiosession avaudiorecorder avaudioengine
1个回答
0
投票

您看到的错误 (

561145187
) 是
AVAudioSessionErrorCodeCannotStartRecording

这些天我认为只有当你的

AVAudioSession.InterruptionOptions
包含
.shouldResume
时才允许你重新启动音频 IO。 这并不总是发生!

如果这是一个问题,您可以尝试通过向

.mixWithOthers
类别选项添加
AVAudioSession
来减少被打扰的可能性。在这种情况下,您将需要
.playAndRecord
而不是
.record

© www.soinside.com 2019 - 2024. All rights reserved.