【发布时间】:2015-03-20 14:03:18
【问题描述】:
我的应用使用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?
【问题讨论】:
-
这个问题你解决了吗?
-
我有另一个分支,我已将
MPMusicPlayerController完全替换为AVAudioPlayer。它可以工作,但意味着你的控制器需要处理更多的逻辑。
标签: ios xcode ipod mpmusicplayercontroller