AudioKit iOS:如何将输入节点动态连接到活动链中的混音器?

Ste*_* D. 6 ios swift audiokit

如何在 AudioKit iOS 中将输入节点动态连接到活动链中的混音器?

环境:AudioKit 4.3、Swift 4.1、Xcode 9.4.1、iOS 11.4。

问题

我正在构建一个应用程序,其中包含由 AKNode 对象链组成的动态模块。这些模块根据请求动态连接到正在运行的 AudioKit 引擎的专用 AKMixer 节点或从其分离。这很有效,除非尝试连接任何包含输入节点(例如 AKMicrophone 或 AKStereoInput)的模块,否则会导致崩溃:

2018-06-14 10:13:33.696384-0700 MyApp[3440:2578936] [mcmx] 338: 输入总线 0 采样率为 0 2018-06-14 10:13:33.6967049:MyApp3[0704e] [mcmx] ] AVAEInternal.h:103:_AVAE_CheckNoErr: [AVAudioEngineGraph.mm:3632:UpdateGraphAfterReconfig: (AUGraphParser::InitializeActiveNodesInOutputChain(ThisGraph, kOutputChainFullTraversal, *conn.srcNode, isChainActive)): 错误 -10875-13070:10875-1307 -0700 DynamicMic[3440:2578936] *** 由于未捕获的异常“com.apple.coreaudio.avfaudio”而终止应用程序,原因:“错误 -10875”

或者,调用AudioKit.stop(),然后执行有问题的连接,然后调用AudioKit.start()无法启动 AudioKit,但它避免了崩溃:

AKMicrophone.swift:init():45:Mixer 输入 8 2018-06-14 10:16:09.532277-0700 MyApp[3443:2580588] [mcmx] 338:输入总线 0 采样率为 0 2018-06-1 16:09.532603-0700 MyApp[3443:2580588] [avae] AVAEInternal.h:103:_AVAE_CheckNoErr: [AVAudioEngineGraph.mm:1265:Initialize: (err = AUGraphParser::InitializeActiveNodesInOutputChain),GraphActiveNodesInOutputChain() ): 错误 -10875 2018-06-14 10:16:09.532654-0700 MyApp[3443:2580588] [avae] AVAudioEngine.mm:149:-[AVAudioEngine prepare]: Engine@0x1c0008010, 错误 =10010: 无法初始化2018-06-14 10:16:09.651495-0700 MyApp[3443:2580588] [mcmx] 338: 输入总线 0 采样率为 0 2018-06-14 10:16:09.6515049:MyApp25048e[0378e] ] AVAEInternal.h:103:_AVAE_CheckNoErr: [AVAudioEngineGraph.mm:1265:Initialize:(err = AUGraphParser::InitializeActiveNodesInOutputChain(ThisGraph, kOutputChainOptimizedTraversal, *GetOutputNode(), isOutputChainActive)): 错误 -10875

唯一有效的方法是以静态方式制作整个音频节点图,包括 AKMicrophone 节点,设置输出节点,然后只启动一次 AudioKit。但是,这种方法无法满足我的应用程序所需的动态音频节点图的要求。

代码

这是托管 AudioKit 类的精简版。理想情况下AudioEngine.start(),在 AppDelegatedidFinishLaunchingWithOptions方法等入口点调用。

import Foundation
import AudioKit

class AudioEngine {

    private static var _mainMixer: AKMixer = AKMixer()

    // Connected main mixer input nodes.
    private static var _mainMixerNodes = [AKNode]()

    private static var _isInited = false
    private static var _isStarted = false

    static func start() {
        if !_isInited {
            // Clean tempFiles !
            AKAudioFile.cleanTempDirectory()

            // Session settings
            AKSettings.bufferLength = .medium

            do {
                try AKSettings.setSession(category: .playAndRecord, with: .allowBluetoothA2DP)
            } catch {
                AKLog("Could not set session category.")
            }

            AKSettings.defaultToSpeaker = true
            _isInited = true
        }
        if !_isStarted {
            AudioKit.output = _mainMixer
            print("AudioEngine start: just set output to global mixer")
            do {
                try AudioKit.start()
                AKLog("AudioEngine: AudioKit started")
                _isStarted = true
            } catch {
                AKLog("AudioEngine: AudioKit could not start")
            }
        }
    }

    static func stop() {
        if _isStarted {
            AudioKit.output = nil
            do {
                try AudioKit.stop()
                AKLog("AudioEngine: AudioKit stopped")
                _isStarted = false
            } catch {
                AKLog("AudioEngine: AudioKit could not stop")
            }
        }
    }

    static func connect(_ node: AKNode) {
        if !_mainMixerNodes.contains(node) {
            _mainMixer.connect(input: node)
            _mainMixerNodes.append(node)
        }
    }

    static func disconnect(_ node: AKNode) {
        if let nodeIndex = _mainMixerNodes.index(of: node) {
            node.detach()
            _mainMixerNodes.remove(at: nodeIndex)
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

稍后在应用程序流程中,将打开一个自定义视图,该视图通过输入节点 (AKMicrophone) 使用麦克风。这就是问题发生的地方。这是一个精简版:

import UIKit

import AudioKit

class MicViewController: UIViewController {

    let mic = AKMicrophone()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Approach 1: Causes a crash.
        AudioEngine.connect(mic)

        // Approach 2: Stop engine, connect, start engine again. Does not work.
        // AudioEngine.stop()
        // AudioEngine.connect(mic)
        // AudioEngine.start()
    }

}
Run Code Online (Sandbox Code Playgroud)

dav*_*234 3

In disconnect, detach will detach the node from the underlying AVAudioEngine. AKMicrophone's underlying node it's a property of AVAudioEngine, so it's probably better to just disconnect it.

let disconnect = node is AKMicrophone ? disconnectOutput : detach
node.disconnect()
Run Code Online (Sandbox Code Playgroud)

But muting it is easier.

mic.volume = 0
Run Code Online (Sandbox Code Playgroud)

  • 这是一个很棒的提示,我已将其放入我的代码中。但是,它并不能解决 AKMicrophone 节点与正在运行的 AKMixer 的 connect() 时发生的问题。 (2认同)