且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

AudioPlayer和锁屏/控制中心控件Swift

更新时间:2023-02-03 10:09:00

您需要使用MPRemoteCommandCenter来执行此操作.例如,在您的viewControllers viewDidLoad()中,您可以添加以下内容:

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
    }

}

只需更改我包含的评论即可更新您的按钮UI.如果尚未导入MediaPlayerMediaPlayer.framework ,则还需要导入.

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.