AudioPlayer and lockscreen/control center control Swift [closed]

最后都变了- 提交于 2019-12-21 15:15:43

问题


I'm new on Swift. I write because I want to ask a question. Me and my friend we are developing an audio player, but we have a problem. The player also works in background and remote controls from the lockscreen and the control center work, but if music is interrupted by one of these two controls, the play/pause button of our player is not updated with the correct icon. My question is, how can I make it clear to the player that the music was starting/stopping by one of the remote controls and the player to act accordingly, changing the icon of play/pause button? thanks a lot, I hope I was clear.


回答1:


You need to use the MPRemoteCommandCenter to do this. For example in your viewControllers viewDidLoad() you can add this:

override func viewDidLoad() {
    super.viewDidLoad()

    UIApplication.shared.beginReceivingRemoteControlEvents()
    let commandCenter = MPRemoteCommandCenter.shared()

    commandCenter.pauseCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in
        //Update your button here for the pause command
        return .success
    }

    commandCenter.playCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in
        //Update your button here for the play command
        return .success
    }

}

Just change the comments I have included to update your buttons UI. You will also need to import MediaPlayer and MediaPlayer.framework if you have not done so already.



来源:https://stackoverflow.com/questions/42897354/audioplayer-and-lockscreen-control-center-control-swift

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