【问题标题】:Swift: disconnect an AVAudioUnit from playing AVAudioEngineSwift:断开 AVAudioUnit 与播放 AVAudioEngine 的连接
【发布时间】:2019-05-14 06:52:36
【问题描述】:

在某些时候,我正在播放带有一些效果的音频文件。这是代码:

 engine = AVAudioEngine()
    playerB = AVAudioPlayerNode()

    playerB.volume = 0.5

    let path = Bundle.main.path(forResource: "ukulele", ofType: "wav")!
    let url = NSURL.fileURL(withPath: path)

    let file = try? AVAudioFile(forReading: url)
    buffer = AVAudioPCMBuffer(pcmFormat: file!.processingFormat, frameCapacity: AVAudioFrameCount(file!.length))!
    try! file!.read(into: buffer)

    reverb.loadFactoryPreset(AVAudioUnitReverbPreset.cathedral)
    reverb.wetDryMix = 50

    distortion.loadFactoryPreset(AVAudioUnitDistortionPreset.speechRadioTower)
    distortion.wetDryMix = 25

    let delay = AVAudioUnitDistortion()
    delay.loadFactoryPreset(AVAudioUnitDistortionPreset.speechAlienChatter)
    delay.wetDryMix = 25


    engine.attach(playerB)
    engine.attach(reverb)
    engine.attach(distortion)
    engine.attach(delay)
    engine.attach(pitch)
    engine.attach(speedControl)

    engine.connect(playerB, to: pitch, format: nil)

    engine.connect(pitch, to: speedControl, format: nil)
    engine.connect(speedControl, to: reverb, format: nil)

    engine.connect(reverb, to: distortion, format: nil)
    engine.connect(distortion, to: engine.mainMixerNode, format: buffer.format)

    playerB.scheduleBuffer(buffer, at: nil, options: AVAudioPlayerNodeBufferOptions.loops, completionHandler: nil)



    engine.prepare()
    try! engine.start()

我想要的是在发生特定操作时断开AVAudioUnit 之一。但是,删除AVAudioUnit 后,播放器完全静音。

例如,如果我想删除reverb,代码是:engine.disconnectNodeOutput(reverb) 但是在这条线运行之后,玩家是沉默的。

我做错了什么?我只是想删除一个已经添加的效果。

【问题讨论】:

    标签: swift disconnect avaudioengine avaudioplayernode


    【解决方案1】:

    这是一条链条,而您破坏了其中一个链条。这是你的代码:

    engine.connect(pitch, to: speedControl, format: nil)
    engine.connect(speedControl, to: reverb, format: nil)
    engine.connect(reverb, to: distortion, format: nil)
    engine.connect(distortion, to: engine.mainMixerNode, format: buffer.format)
    

    如果你移除混响节点,现在速度控制和失真之间会有一个漏洞。没有什么能越过那个洞。你需要连接它们。

    【讨论】:

    • 所以你的意思是每次我想删除一个效果,我都应该重新连接所有的链条?但是这样我就不能继续播放音频了。它将:停止播放,再次实例化连接,再次播放音频。这样我就可以听到音频的断线/连线了,还有其他方法吗?
    • 先连接它们。
    • 每当我engine.connect(pitch, to: speedControl, format: nil) engine.connect ... 播放器停止播放时
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-14
    • 2016-02-22
    • 2019-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多