Reputation: 1650
I have the following method that plays a file.
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)");
}
}
The file plays fine, but the pan value has no effect. I've tried different values (-1, 1, -100, +100) and for all of these the stereo playback remains in the middle.
What do I need to do to make panning work?
Upvotes: 1
Views: 796
Reputation: 302
Try setting the "pan" property on the AVAudioSourceNode (using values -1 to 1) after calling the "start" function on AVAudioEngine. Setting the "pan" value before calling "start" had no affect for me.
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
Upvotes: 1
Reputation: 535306
I think the problem is that the main mixer node is not usable in that way. You should introduce another mixer node and set its pan
, or set the pan
directly on the AVAudioPlayerNode.
Upvotes: 2