0

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!

Marwen Doukh
  • 1,946
  • 17
  • 26
ikcd8
  • 1
  • 2

1 Answers1

0

It`s your code to start playing AVaudioPlayerNode. But, where is your code to stop playing AVaudioPlayerNode?

Have you seen this?

Mike
  • 51
  • 1
  • 8
  • the user selects the song from their tableView to play. this opens the new view controller which is the Music Player. when the user goes back to tableView and selects another song, both songs are playing. I don't want to play the first song after selecting the second song from my tableView cell. Is there any way to stop the player node in a class in which it was not initialized. – ikcd8 Nov 14 '19 at 09:16
  • If its a part of business logic then, as a programmer, its your responsibility to track it work. So, you should know what object is playing and stop it if you need it. – Mike Nov 14 '19 at 17:09
  • Thank you for your time. I found the answer and I'll post it soon. – ikcd8 Nov 19 '19 at 09:01