首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用MPMusicPlayerController时无法修改MPRemoteCommandCenter操作

使用MPMusicPlayerController时无法修改MPRemoteCommandCenter操作
EN

Stack Overflow用户
提问于 2015-03-20 22:03:19
回答 1查看 1.3K关注 0票数 3

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

用户可以设置自定义currentPlaybackRate。这与预期的一样。

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

为了将currentPlaybackRate设置为适当的值,我尝试覆盖MPRemoteCommandCenter事件:

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

发布于 2015-03-22 00:12:04

如果你真的不需要MPMusicPlayerController,尝试使用这个解决方案,这个人结合了AVAudioPlayer和MPMusicPlayerController的行为。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29168373

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档