我想构建一个广播应用程序,所以我想使用停止按钮而不是控制中心的暂停按钮,就像 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
)。
所以问题是:如何使用那个停止按钮?谢谢你。