I have 2 ViewControllers which each play a diffrent AVAudioPlayer. Each one needs a diffrent set of commands for the MPRemoteCommandCenter. Each ViewController has something like this:
let remoteCommandCenter = MPRemoteCommandCenter().shared
func refreshCommandCenterButtons() {
remoteCommandCenter.previousTrackCommand.isEnabled = true
remoteCommandCenter.nextTrackCommand.isEnabled = true
remoteCommandCenter.playCommand.isEnabled = true
remoteCommandCenter.pauseCommand.isEnabled = true
remoteCommandCenter.playCommand.addTarget(self, action: #selector(self.playButtonTapped))
remoteCommandCenter.pauseCommand.addTarget(self, action: #selector(self.pauseButtonTapped))
remoteCommandCenter.nextTrackCommand.addTarget(self, action: #selector(self.nextButtonTapped))
remoteCommandCenter.previousTrackCommand.addTarget(self, action: #selector(self.previousButtonTapped))
remoteCommandCenter.togglePlayPauseCommand.addTarget(self, action: #selector(self.playPauseAction))
}
override func viewDidLoad() {
super.viewDidLoad()
refreshCommandCenterButtons()
}
Whenever a user plays a song from FirstViewController, and then navigate to the SecondViewController, the FirstViewContoller's audioPlayer pauses. The problem is that once a user navigates from FirstViewController to the Second, and presses the play button on the Second, it plays both audioPlayers.
I tried adding this to the refreshCommandCenterButtons() function. This would be in the SecondViewController:
remoteCommandCenter.pauseCommand.removeTarget(#selector(FirstViewController().pauseButtonPressed))
remoteCommandCenter.playCommand.removeTarget(#selector(FirstViewController().playButtonPressed))
remoteCommandCenter.togglePlayPauseCommand.removeTarget(#selector(FirstViewController().playPauseTapped))
But nothing changed. What do I do? Thanks for the help!