AudioPlayer and lockscreen/control center control Swift [closed]

南笙酒味 提交于 2019-12-04 07:31:18
totiG

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.

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