使用 MPMusicPlayerController 时无法修改 MPRemoteCommandCenter 操作 [英] Can't Modify MPRemoteCommandCenter Actions when using MPMusicPlayerController

查看:29
本文介绍了使用 MPMusicPlayerController 时无法修改 MPRemoteCommandCenter 操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My app uses MPMusicPlayerController.systemMusicPlayer() to play audio which works well.

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

The user is able to set a custom currentPlaybackRate. This works as expected.

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

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.

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

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

MPRemoteCommandCenter 是否应该在使用 MPMusicPlayerController 时尊重 addTargetWithHandler?

Is MPRemoteCommandCenter supposed to respect addTargetWithHandler when using MPMusicPlayerController?

推荐答案

如果你真的不需要 MPMusicPlayerController 试试这个方案https://github.com/gangverk/GVMusicPlayerController这家伙结合了 AVAudioPlayer 和 MPMusicPlayerController 的行为.

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.

但我确实需要一个答案.因为在我的应用中我真的需要 MPMusicPlayerController.

But I steel need for an answer. Because in my app I really need MPMusicPlayerController.

这篇关于使用 MPMusicPlayerController 时无法修改 MPRemoteCommandCenter 操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆