I am creating a music player application and everything is working fine. I still use the system music player, but now I want to switch to using the music player of the application, as well as where I ran into problems - for life I canβt figure out how to get my play / pause, which is called from the iOS control center . Here is the code that I use in my main view controller:
override func viewDidLoad() { super.viewDidLoad() self.musicPlayer = MPMusicPlayerController.applicationMusicPlayer() self.registerForMediaPlayerNotifications() UIApplication.sharedApplication().beginReceivingRemoteControlEvents() let commandCenter = MPRemoteCommandCenter.sharedCommandCenter() commandCenter.previousTrackCommand.enabled = false commandCenter.previousTrackCommand.addTarget(self, action: "previousTrack") commandCenter.nextTrackCommand.enabled = false commandCenter.nextTrackCommand.addTarget(self, action: "nextTrack") commandCenter.togglePlayPauseCommand.enabled = true commandCenter.togglePlayPauseCommand.addTarget(self, action: "playOrPauseMusic") commandCenter.pauseCommand.addTarget(self, action: "playOrPauseMusic") commandCenter.pauseCommand.enabled = true commandCenter.playCommand.addTarget(self, action: "playOrPauseMusic") commandCenter.playCommand.enabled = true [...] } func previousTrack() { } func nextTrack() { } func playOrPauseMusic() { print("playOrPause") }
source share