Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AudioPlayer and lockscreen/control center control Swift [closed]

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.

like image 417
user7738251 Avatar asked Mar 20 '17 06:03

user7738251


1 Answers

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.

like image 185
totiDev Avatar answered Nov 11 '22 17:11

totiDev