7

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

在此处输入图像描述

这是我在 RadioPlayer 课程中所做的:

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)。

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

4

2 回答 2

9

编辑: 我认为“停止”命令仅在MPNowPlayingInfoPropertyIsLiveStream = true(仅适用于 iOS 10)/:禁用“暂停”或“切换播放暂停”命令时才显示。从 iOS 10 开始,如果MPNowPlayingInfoPropertyIsLiveStream = true. 您可能还需要处理“暂停”或“切换播放暂停”命令(对于早期版本)。祝你好运!

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

斯威夫特 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
        }

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

于 2017-02-08T05:37:35.643 回答
-1

根据这个问题答案,显然 ControlCenter 是不可定制的(至少到现在为止)。

于 2016-11-15T09:57:59.503 回答