如何使用 iOS 控制中心中的停止按钮代替 swift 中的暂停按钮

How to use Stop button in iOS control center instead of pause button with swift

我想构建一个广播应用程序,所以我想在控制中心使用停止按钮而不是暂停按钮,就像 Apple Radio 在本机音乐应用程序中所做的那样:

这是我在 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 {

    }
}

它现在工作正常,但没有显示停止按钮。相反,我有暂停按钮,这对广播播放器没有意义哈哈。

请注意,在上述情况下,即使控制中心显示暂停按钮,按下暂停按钮时也不会发生任何事情,因为没有附加目标(我将其附加到 stopCommand) .

所以问题是:如何使用停止按钮?谢谢。

根据 this question-answer ,显然 ControlCenter 不可自定义(至少到现在为止)。

编辑: 我认为 "stop" 命令仅在 MPNowPlayingInfoPropertyIsLiveStream = true 时显示(仅适用于 iOS 10)/: 禁用 "pause" 或 "togglePlayPause" 命令并不重要。从iOS10开始,如果MPNowPlayingInfoPropertyIsLiveStream = true就会显示"stop"命令。 您可能还需要处理 "pause" 或 "togglePlayPause" 命令(对于早期版本)。祝你好运!

好吧,我也有这个疑问,并没有在互联网上找到如何做我想做的事情,所以我开始阅读更多关于 MPRemoteCommandCenterMPNowPlayingInfoCenter 的内容。 我尝试禁用所有我没有使用的按钮。此外,我阅读了有关 MPNowPlayingInfoPropertyIsLiveStream 的内容,并分享了此 post 以防有人发现它有用(查看代码中的注释):

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 {
                [=11=].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
        }

我已经完成了。希望对您和其他人有所帮助 (: