Dav*_*vid 7 ios control-center swift
我想构建一个电台应用程序,因此我想使用控制中心中的“停止”按钮而不是“暂停”按钮,就像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 {
}
}
Run Code Online (Sandbox Code Playgroud)
现在工作正常,但没有显示停止按钮。相反,我有“暂停”按钮,这对广播播放器没有意义。
请注意,在上述情况下,即使控制中心显示了暂停按钮,按下暂停按钮时也不会发生任何事情,因为没有目标物附着(我将其附着到stopCommand)。
所以问题是:如何使用“停止”按钮?谢谢。
编辑:
我认为“停止”命令仅在MPNowPlayingInfoPropertyIsLiveStream = true(仅可从iOS 10获得)/时显示:/禁用“暂停”或“ togglePlayPause”命令并不重要。在iOS 10中,如果显示“停止”命令MPNowPlayingInfoPropertyIsLiveStream = true。您可能还需要处理“ pause”或“ togglePlayPause”命令(对于早期版本)。祝好运!
好的,我也有这个疑问,在互联网上找不到如何做自己想做的事,所以我开始阅读有关MPRemoteCommandCenter和的更多信息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
Run Code Online (Sandbox Code Playgroud)
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
}
Run Code Online (Sandbox Code Playgroud)
我已经做好了。希望我对您和其他人有所帮助(:
| 归档时间: |
|
| 查看次数: |
2271 次 |
| 最近记录: |