【问题标题】:AVAudioEngine mainMixerNode pan not workingAVAudioEngine mainMixerNode pan 不工作
【发布时间】:2020-03-27 13:06:24
【问题描述】:

我有以下播放文件的方法。

let filePath = "/Users/fractor/Desktop/TestFile.mp3"
var file : AVAudioFile?
var audioEngine = AVAudioEngine()
var playerNode = AVAudioPlayerNode()

@IBAction func play(_ sender: Any) {

    do {
        file = try AVAudioFile(forReading: URL(fileURLWithPath: filePath))
    } catch let error {
        print(error.localizedDescription)
        return
    }

    audioEngine.attach(playerNode)
    audioEngine.connect(playerNode, to: audioEngine.mainMixerNode, format: file!.processingFormat);

    do {
        try audioEngine.start()
    } catch let error {
        print(error.localizedDescription)
        return
    }

    audioEngine.mainMixerNode.pan = 100 // No effect

    playerNode.scheduleFile(file!, at: nil){}

    DispatchQueue.global(qos: .background).async {
        print("audioEngine.isRunning = \(self.audioEngine.isRunning)");
        self.playerNode.play();
        print("playerNode.isPlaying = \(self.playerNode.isPlaying)");
    }
}

文件播放正常,但平移值无效。我尝试了不同的值(-1、1、-100、+100),对于所有这些,立体声播放都保持在中间。

我需要做什么才能使平移工作?

【问题讨论】:

  • 是的 pan 被记录在 -1 和 1 之间。但是,this post 似乎表明文档和现实之间可能存在差异。正如我所说,我已经尝试了许多值,包括记录范围的极端值。
  • 我没有注意到 AVAudioPlayerNode 上的平移,这确实有效,谢谢。
  • 添加了缺少的声明。
  • 帖子似乎是正确的。 -100 到 100 似乎是正确的范围。
  • 不知道为什么 pan 不能在混音器上工作,但你解决了我的问题。平移现在有效。一定要把它作为答案。

标签: swift macos avfoundation avaudioengine


【解决方案1】:

我认为问题在于主混音器节点不能以这种方式使用。你应该引入另一个混音器节点并设置它的pan,或者直接在AVAudioPlayerNode上设置pan。

【讨论】:

  • 我直接在我的播放器节点上设置了平移。同样的事情......没有骰子!
  • 对我有用的是在 AVAudioEngine 上调用“start”函数后在 AVAudioSourceNode 上设置“pan”属性(使用值 -1 到 1)。在调用“start”之前设置“pan”值没有影响。
  • @bberak 酷,把它作为一个实际的答案!
【解决方案2】:

尝试在 AVAudioSourceNode 上设置“pan”属性(使用值 -1 到 1)在调用 AVAudioEngine 上的“start”函数之后。在调用“start”之前设置“pan”值对我没有影响。

let audioEngine = AVAudioEngine()
let sourceNode = AVAudioSourceNode { /* ... */ }
let mainMixer = audioEngine.mainMixerNode
let outputNode = audioEngine.outputNode
let format = outputNode.inputFormat(forBus: 0)
let inputFormat = AVAudioFormat(commonFormat: format.commonFormat,
                                        sampleRate: format.sampleRate,
                                        channels: 1,
                                        interleaved: format.isInterleaved)

audioEngine.attach(sourceNode)
audioEngine.connect(sourceNode, to: mainMixer, format: inputFormat)

do {
   try audioEngine.start()
} catch {
   print("Could not start engine: \(error.localizedDescription)")
}

sourceNode.pan = -1 // -1 to 1

【讨论】:

    猜你喜欢
    • 2016-02-22
    • 1970-01-01
    • 1970-01-01
    • 2019-01-24
    • 1970-01-01
    • 2014-07-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多