AudioPlayer和锁屏/控制中心控件Swift [英] AudioPlayer and lockscreen/control center control Swift
问题描述
我是Swift的新手.我写是因为我想问一个问题.我和我的朋友,我们正在开发音频播放器,但是有一个问题.播放器还可以在锁定屏幕的后台和遥控器中工作,而控制中心也可以工作,但是如果音乐被这两个控件之一打断,则播放器的播放/暂停"按钮不会使用正确的图标更新. 我的问题是,如何通过一个遥控器使播放器清楚地知道音乐正在开始/停止,并且播放器相应地采取了行动,更改了播放/暂停按钮的图标?非常感谢,我希望我很清楚.
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.
推荐答案
您需要使用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.如果尚未导入MediaPlayer
和MediaPlayer.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.
这篇关于AudioPlayer和锁屏/控制中心控件Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!