I am trying to develop a music player app with equaliser. I am using AVAudioEngine
and attaching AVAudioPlayerNode
but when I change the song the previous song is also playing with the current song. Changing multiple song, plays all the song simultaneously. previously I was using MPMediaItemPlayer
but in that case I was not able to attach equaliser. I want my app to play one song at a time with equaliser.
self.AVaudioEngine = AVAudioEngine.init()
self.AVaudioPlayerNode = AVAudioPlayerNode.init()
self.AVaudioUnitEQ = AVAudioUnitEQ(numberOfBands: 10)
self.AVaudioEngine.attach(self.AVaudioPlayerNode)
self.AVaudioEngine.attach(self.AVaudioUnitEQ)
self.AVaudioPlayerNode.scheduleFile(self.AVaudioFile, at: nil, completionHandler: nil)
self.AVaudioEngine.connect(self.AVaudioPlayerNode, to: self.AVaudioUnitEQ, format: self.AVaudioFile.processingFormat)
self.AVaudioEngine.connect(self.AVaudioUnitEQ, to: self.AVaudioEngine.mainMixerNode, format: self.AVaudioFile.processingFormat)
if !self.AVaudioEngine.isRunning {
try! self.AVaudioEngine.start()
}
let sampleRate = self.AVaudioFile.processingFormat.sampleRate / 2
let format = self.AVaudioEngine.mainMixerNode.outputFormat(forBus: 0)
self.AVaudioEngine.mainMixerNode.installTap(onBus: 0, bufferSize: AVAudioFrameCount(sampleRate), format: format, block:{ (buffer: AVAudioPCMBuffer!, time: AVAudioTime!) -> Void in
})
self.AVaudioPlayerNode.play()
Thanks in advance!