我有 2 个ViewControllers
,每个都播放不同的AVAudioPlayer
. 每个人都需要一组不同的命令MPRemoteCommandCenter
。每个 ViewController 都有这样的东西:
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()
}
每当用户从 播放歌曲FirstViewController
,然后导航到 时SecondViewController
,FirstViewContoller's
audioPlayer 就会暂停。问题在于,一旦用户从第二台导航FirstViewController
到第二台并按下第二台上的播放按钮,它就会同时播放两个音频播放器。
我尝试将其添加到refreshCommandCenterButtons()
函数中。这将在SecondViewController
:
remoteCommandCenter.pauseCommand.removeTarget(#selector(FirstViewController().pauseButtonPressed))
remoteCommandCenter.playCommand.removeTarget(#selector(FirstViewController().playButtonPressed))
remoteCommandCenter.togglePlayPauseCommand.removeTarget(#selector(FirstViewController().playPauseTapped))
但什么都没有改变。我该怎么办?谢谢您的帮助!