锁屏 iPhone 上的音乐控制
Posted
技术标签:
【中文标题】锁屏 iPhone 上的音乐控制【英文标题】:Music control at lock screen iPhone 【发布时间】:2017-03-17 06:06:40 【问题描述】:我正在开发音乐应用程序,但对于锁屏控制,我无法指定持续时间和经过的时间
这是我的代码
let commandCenter = MPRemoteCommandCenter.shared()
commandCenter.previousTrackCommand.isEnabled = true;
commandCenter.previousTrackCommand.addTarget(self, action:#selector(home_ViewController.btn_rewind(_:)))
commandCenter.nextTrackCommand.isEnabled = true
commandCenter.nextTrackCommand.addTarget(self, action:#selector(home_ViewController.btn_fast(_:)))
commandCenter.playCommand.isEnabled = true
commandCenter.playCommand.addTarget(self, action:#selector(home_ViewController.play_player))
commandCenter.pauseCommand.isEnabled = true
commandCenter.pauseCommand.addTarget(self, action:#selector(home_ViewController.pause_player))
commandCenter.togglePlayPauseCommand.isEnabled = true
commandCenter.togglePlayPauseCommand.addTarget(self, action:#selector(home_ViewController.togglePlay_Pause))
commandCenter.skipBackwardCommand.isEnabled = false
commandCenter.skipForwardCommand.isEnabled = false
if #available(ios 9.1, *)
commandCenter.changePlaybackPositionCommand.isEnabled = true
else
// Fallback on earlier versions
return
和媒体信息
func setLockInfo()
let url = URL(string: song_UrlString)
let data = try? Data(contentsOf: url!)
let art = MPMediaItemArtwork.init(image: UIImage(data: data!)!)
let songInfo :[String : Any] = [MPMediaItemPropertyTitle :st_title,MPMediaItemPropertyArtwork : art]
MPNowPlayingInfoCenter.default().nowPlayingInfo = songInfo
我正在获取标题和图片,但锁屏不显示时间
我正在使用 SWIFT 3 编码
【问题讨论】:
【参考方案1】:它没有显示时间,因为你没有告诉它显示时间。
要显示播放时间,您的 nowPlayingInfo
字典需要包含键的值:
MPNowPlayingInfoPropertyElapsedPlaybackTime
,所以它知道你开始播放的当前时间,
MPMediaItemPropertyPlaybackDuration
,所以它知道当前时间是相对于条中的什么,并且
MPNowPlayingInfoPropertyPlaybackRate
,所以它可以自动更新播放时间UI,无需您定期设置当前时间。
如果您希望播放时间栏具有交互性(即,允许跳转到其他时间而不是仅显示当前时间,请在远程指挥中心注册changePlaybackPositionCommand
。
【讨论】:
让 songInfo :[String : Any] = [MPMediaItemPropertyTitle :st_title,MPMediaItemPropertyArtwork : art,MPNowPlayingInfoPropertyElapsedPlaybackTime : NSNumber.init(value: (player.currentTime()).value),MPMediaItemPropertyPlaybackDuration : NSNumber.init (value: ((player.currentItem?.duration)?.value)!),MPNowPlayingInfoPropertyPlaybackRate :NSNumber.init(value: 1)] 我已经改到上面了,但是还没来 能否举个例子【参考方案2】:Swift 3 示例,所有命令都在工作,包括 changePlaybackPositionCommand:
func remotePlayerInit()
UIApplication.shared.beginReceivingRemoteControlEvents()
let commandCenter = MPRemoteCommandCenter.shared()
commandCenter.pauseCommand.addTarget(self, action: #selector(self.pauseSongTouch(_:)))
commandCenter.playCommand.addTarget(self, action: #selector(self.playSongTouch(_:)))
commandCenter.nextTrackCommand.addTarget(self, action: #selector(self.nextSongTouch(_:)))
commandCenter.previousTrackCommand.addTarget(self, action: #selector(self.previousSongTouch(_:)))
commandCenter.changePlaybackPositionCommand.addTarget(self, action: #selector(self.changedThumbSlider(_:)))
setLockInfo()
func changedThumbSlider(_ event: MPChangePlaybackPositionCommandEvent) -> MPRemoteCommandHandlerStatus
audioPlayer.currentTime = event.positionTime
setLockInfo()
return .success
func setLockInfo()
let image = PlayerVC.songs[PlayerVC.currentSelection].image
let artwork = MPMediaItemArtwork.init(boundsSize: image.size, requestHandler: (size) -> UIImage in
return image
)
let songInfo: [String: Any] = [MPMediaItemPropertyTitle: PlayerVC.songs[PlayerVC.currentSelection].name,
MPMediaItemPropertyArtwork: artwork,
MPNowPlayingInfoPropertyElapsedPlaybackTime: TimeInterval(audioPlayer.currentTime),
MPNowPlayingInfoPropertyPlaybackRate: 1,
MPMediaItemPropertyPlaybackDuration: audioPlayer.duration,
MPMediaItemPropertyArtist: "Artist"]
MPNowPlayingInfoCenter.default().nowPlayingInfo = songInfo
【讨论】:
以上是关于锁屏 iPhone 上的音乐控制的主要内容,如果未能解决你的问题,请参考以下文章
当来自锁屏 iPhone 时在 didreceivelocalnotification 上重绘 GUI