AVAudioEngine mainMixerNode 声像不工作

AVAudioEngine mainMixerNode pan not working

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

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,或者直接在 AVAudioPlayerNode 上设置 pan

尝试在 AVAudioSourceNode 上设置“pan”属性(使用值 -1 到 1)after 调用AVAudioEngine 上的“开始”功能。在调用“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