【问题标题】:How to use AVAudioNodeTapBlock in a tap in AVAudioEngine.如何在 AVAudioEngine 的 Tap 中使用 AVAudioNodeTapBlock。
【发布时间】:2017-01-23 11:01:43
【问题描述】:

我正在尝试在 AVAudioEngine 上安装水龙头。我有当前代码:

    guard let engine = engine, let input = engine.inputNode else {
        print("error!")
        return
    }
    let format = input.inputFormat(forBus: 0)
    let bufferSize = 4096
    input.installTap(onBus: 0, bufferSize: AVAudioFrameCount(bufferSize), format: format, block: )

我不确定该块中发生了什么。这方面的文档不多。我发现了这个:https://developer.apple.com/reference/avfoundation/avaudionodetapblock?language=objc

有人能解释一下如何使用吗?

谢谢,

费拉斯 A.

【问题讨论】:

    标签: swift avaudioengine


    【解决方案1】:

    如果你想用 Swift 编写,最好查看Swift version of the reference

    声明

    typealias AVAudioNodeTapBlock = (AVAudioPCMBuffer, AVAudioTime) -> Void
    

    您需要传递一个带有两个参数且不返回任何内容的闭包,因此您可以将其写为:

        input.installTap(onBus: 0, bufferSize: AVAudioFrameCount(bufferSize), format: format, block: {buffer, when in
            //...
        })
    

    bufferwhen这两个参数的类型分别是AVAudioPCMBufferAVAudioTime

    所以,如果你想将敲击的音频录制到音频文件中,你可以这样写:

        input.installTap(onBus: 0, bufferSize: AVAudioFrameCount(bufferSize), format: format, block: {buffer, when in
            do {
                try self.audioFile?.write(from: buffer)
            } catch {
                print(error)
            }
        })
    

    (假设audioFileAVAudioFile? 类型的实例属性。)

    无论如何,你需要知道如何使用AVAudioPCMBuffer


    我不确定input.inputFormat(forBus: 0) 是否适合您的情况,但这可能是另一个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-02
      • 2023-01-28
      • 1970-01-01
      相关资源
      最近更新 更多