Mustafa
Mustafa

Reputation: 263

issue with the MPRemoteCommandCenter in swift3

i have an issue for controlling on media from the command center in swift 3 i found solution on the stackoverflow web but it's can't give me control on the video track or audio from background the following is my code :

        override func viewDidLoad() {
        super.viewDidLoad()
        let mpRemoteControlCenter = MPRemoteCommandCenter.shared()
        mpRemoteControlCenter.nextTrackCommand.isEnabled = true
        mpRemoteControlCenter.nextTrackCommand.addTarget(self, action: #selector(self.nextTrack))
        mpRemoteControlCenter.previousTrackCommand.isEnabled = true
        mpRemoteControlCenter.previousTrackCommand.addTarget(self, action: #selector(self.prevTrack))
        mpRemoteControlCenter.playCommand.isEnabled = true
        mpRemoteControlCenter.playCommand.addTarget(self, action: #selector(self.play))
}

   func play() {
        print("play")
    }
    func nextTrack(){
        print("next")
    }
    func prevTrack() {
        print("prev")
    }

thanks a lot

Upvotes: 0

Views: 948

Answers (1)

SergeH
SergeH

Reputation: 618

Assuming you're using Xcode 8, you need to turn on Background Modes in Capabilities, and check "Audio, Airplay, and Picture in Picture".

Add this code to your viewDidLoad() as well

let audioSession = AVAudioSession.sharedInstance()

    do {
        try audioSession.setCategory(AVAudioSessionCategoryPlayback)
    } catch {

    }

It should work after doing so.

Upvotes: 1

Related Questions