Can't Modify MPRemoteCommandCenter Actions when using MPMusicPlayerController

不打扰是莪最后的温柔 提交于 2020-01-04 04:12:07

问题


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

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

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.

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

Is MPRemoteCommandCenter supposed to respect addTargetWithHandler when using MPMusicPlayerController?


回答1:


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.

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



来源:https://stackoverflow.com/questions/29168373/cant-modify-mpremotecommandcenter-actions-when-using-mpmusicplayercontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!