MPNowPlayingInfoCenter buttons do not work

0

I am creating a radio app, but when I want my play and pause buttons to work with MPNowPlayingInfoCenter , only play appears. em> and it does not change to pause .

I hope you can help me.

if NSClassFromString("MPNowPlayingInfoCenter") != nil {
    let songInfo: [String:AnyObject] = [
            MPMediaItemPropertyTitle: "Trasnmisión en vivo ",
            MPMediaItemPropertyArtist: "Expande tv",
           ]

    MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo = songInfo
}
do {
    try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
     UIApplication.sharedApplication().beginReceivingRemoteControlEvents()
    print("Receiving remote control events\n")
} catch {
    print("Audio Session error.\n")
}

@IBAction func playButtonPressed(sender: UIButton) {
    toggle()
}

func toggle() {
    if RadioPlayer.sharedInstance.currentlyPlaying() {
        pauseRadio()
    } else {
        playRadio()
    }
}

func playRadio() {
    RadioPlayer.sharedInstance.play()
    playButton.setTitle("Pause radio", forState: UIControlState.Normal)
    playButton.setImage(UIImage(named: "pause.png")!, forState: .Normal)
    print("Play")
}

func pauseRadio() {
    RadioPlayer.sharedInstance.pause()
    playButton.setTitle("Play radio", forState: UIControlState.Normal)
    playButton.setImage(UIImage(named: "play.png")!, forState: .Normal)
    print("Pause")
}
    
asked by alber 15.06.2017 в 21:16
source

1 answer

0

Apparently, what you are missing is implementing the remoteControlReceived(with:) method, which is called when there is an action in the info center .

func remoteControlReceived(with event: UIEvent?) {
    guard let remoteEvent = event, remoteEvent.type == .remoteControl else {
        return
    }

    switch (remoteEvent.subtype) {
    case .remoteControlPause:
        // ...
    case .remoteControlPlay:
        // ...
    case .remoteControlStop:
        // ...
    default: break
    }
}
    
answered by 15.06.2017 в 21:29