使用MPMusicPlayerController时无法修改MPRemoteCommandCenter操作

我的应用程序使用MPMusicPlayerController.systemMusicPlayer()来播放效果很好的音频。

用户可以设置自定义currentPlaybackRate 。 这按预期工作。

如果用户按下锁定屏幕上的操作( MPRemoteCommandCenter ),则currentPlaybackRate将重置为1.这是因为事件直接发送到系统音乐播放器,而不是应用程序控制器。

要将currentPlaybackRate设置为正确的值,我尝试覆盖MPRemoteCommandCenter事件:

 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 } } 

使用MPMusicPlayerControllerMPMusicPlayerController是否应该尊重MPMusicPlayerController

如果你真的不需要MPMusicPlayerController,请尝试使用此解决方案https://github.com/gangverk/GVMusicPlayerController这个人结合了AVAudioPlayer和MPMusicPlayerController的行为。

但我钢铁需要一个答案。 因为在我的应用程序中我真的需要MPMusicPlayerController。