问题
My app uses MPMusicPlayerController.systemMusicPlayer()
to play audio which works well.
The user is able to set a custom currentPlaybackRate
. This works as expected.
If the user presses an action on the lock screen (the MPRemoteCommandCenter
), the currentPlaybackRate
is reset to 1. This is because events are sent directly to the system music player, not the app controller.
To set the currentPlaybackRate
to the proper value, I tried to override MPRemoteCommandCenter
events:
override func viewDidLoad() {
super.viewDidLoad()
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "willEnterForeground:", name: UIApplicationWillEnterForegroundNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "playbackStateChanged:", name: MPMusicPlayerControllerPlaybackStateDidChangeNotification, object: player)
player.beginGeneratingPlaybackNotifications()
let rcc : MPRemoteCommandCenter = MPRemoteCommandCenter.sharedCommandCenter()
rcc.playCommand.addTargetWithHandler { (event: MPRemoteCommandEvent!) -> MPRemoteCommandHandlerStatus in
NSLog("Lock Screen Play Pressed")
return MPRemoteCommandHandlerStatus.Success
}
rcc.togglePlayPauseCommand.addTargetWithHandler { (event: MPRemoteCommandEvent!) -> MPRemoteCommandHandlerStatus in
NSLog("Lock Screen Play/Pause Pressed")
return MPRemoteCommandHandlerStatus.Success
}
}
Is MPRemoteCommandCenter
supposed to respect addTargetWithHandler
when using MPMusicPlayerController
?
回答1:
Try to use this solution if you don't really need MPMusicPlayerController https://github.com/gangverk/GVMusicPlayerController This guy combine behaviors of AVAudioPlayer and MPMusicPlayerController.
But I steel need for an answer. Because in my app I really need MPMusicPlayerController.
来源:https://stackoverflow.com/questions/29168373/cant-modify-mpremotecommandcenter-actions-when-using-mpmusicplayercontroller