我的应用程序使用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
}
}MPRemoteCommandCenter在使用MPMusicPlayerController时是否应该尊重addTargetWithHandler
发布于 2015-03-22 00:12:04
如果你真的不需要MPMusicPlayerController,尝试使用这个解决方案,这个人结合了AVAudioPlayer和MPMusicPlayerController的行为。
但我需要一个答案。因为在我的应用程序中,我真的需要MPMusicPlayerController。
https://stackoverflow.com/questions/29168373
复制相似问题