How to use the Stop button in the iOS control center instead of the pause button with a quick

I want to create a radio application, so I would like to use the stop button instead of the pause button in the control center, for example, Apple Radio in a native music application:

enter image description here

Here is what I did in my RadioPlayer class:

private var shoutcastStream = NSURL(string: "http://shoutcast.com:PORT/;stream.mp3")

var playerItem:AVPlayerItem?
var player:AVPlayer?

let commandCenter = MPRemoteCommandCenter.sharedCommandCenter()

override init() {

    super.init()

    do {

        // Allow background audio
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
        do {
            try AVAudioSession.sharedInstance().setActive(true)
        } catch _ as NSError {

        }

        // Disable Next, Prev and Pause
        commandCenter.pauseCommand.enabled = false
        commandCenter.nextTrackCommand.enabled = false
        commandCenter.previousTrackCommand.enabled = false

        // Enable Play
        commandCenter.playCommand.enabled = true
        commandCenter.playCommand.addTarget(self, action: #selector(RadioPlayer.play))

        // Enable Stop
        commandCenter.stopCommand.enabled = true
        commandCenter.stopCommand.addTarget(self, action: #selector(RadioPlayer.stop))

    } catch _ as NSError {

    }
}

Now it works fine, but the stop button is not displayed. Instead, I have a Pause button, which does not make sense for the radio player haha.

Please note that in the above case, even if the control center shows a pause button, nothing happens when the pause button is pressed because no target is attached to it (I linked it to stopCommand).

So the question is: how to use this "Stop" button? Thank.

+4
2

EDIT: , "stop" , MPNowPlayingInfoPropertyIsLiveStream = true ( iOS 10)/: , "" "togglePlayPause". iOS 10 "stop" , MPNowPlayingInfoPropertyIsLiveStream = true. , "" "togglePlayPause" ( ). !

, , , , MPRemoteCommandCenter MPNowPlayingInfoCenter. , . , MPNowPlayingInfoPropertyIsLiveStream, , - ( ):

Swift 3

MPNowPlayingInfoCenter ( ):

var songInfo = [:] as [String : Any]

if NSClassFromString("MPNowPlayingInfoCenter") != nil {

            songInfo[MPMediaItemPropertyArtwork] = MPMediaItemArtwork(image: UIImage(named: "your_image_name")!)
            songInfo[MPMediaItemPropertyTitle] = "Title"
            songInfo[MPMediaItemPropertyArtist] = "Artist name"

            // If is a live broadcast, you can set a newest property (iOS 10+): MPNowPlayingInfoPropertyIsLiveStream indicating that is a live broadcast
            if #available(iOS 10.0, *) {
                songInfo[MPNowPlayingInfoPropertyIsLiveStream] = true
            } else {
                // Fallback on earlier versions
            }

            MPNowPlayingInfoCenter.default().nowPlayingInfo = songInfo

} // end if MPNowPlayingInfoCenter

MPRemoteCommandCenter:

if #available(iOS 9.1, *) {

            let center = MPRemoteCommandCenter.shared()

            // Disable all buttons you will not use (including pause and togglePlayPause commands)
            [center.pauseCommand, center.togglePlayPauseCommand, center.nextTrackCommand, center.previousTrackCommand, center.changeRepeatModeCommand, center.changeShuffleModeCommand, center.changePlaybackRateCommand, center.seekBackwardCommand, center.seekForwardCommand, center.skipBackwardCommand, center.skipForwardCommand, center.changePlaybackPositionCommand, center.ratingCommand, center.likeCommand, center.dislikeCommand, center.bookmarkCommand].forEach {
                $0.isEnabled = false
            }

            // For "play" command
            center.playCommand.addTarget { (commandEvent) -> MPRemoteCommandHandlerStatus in
                // play the song here
                return MPRemoteCommandHandlerStatus.success
            }

            // For "stop" command
            center.stopCommand.addTarget { (commandEvent) -> MPRemoteCommandHandlerStatus in
                // stop the song here
                return MPRemoteCommandHandlerStatus.success
            }

        } else {
            // Fallback on earlier versions
        }

. , (:

+3

-, , ControlCenter ( ).

0

All Articles