添加通知
// 响应 Remote Control事件
MPRemoteCommandCenter.shared().playCommand.addTarget(self, action: #selector(playOrPause))
MPRemoteCommandCenter.shared().nextTrackCommand.addTarget(self, action: #selector(playNext))
MPRemoteCommandCenter.shared().pauseCommand.addTarget(self, action: #selector(playOrPause))
MPRemoteCommandCenter.shared().previousTrackCommand.addTarget(self, action: #selector(playPre))
响应事件
@objc func playNext()->MPRemoteCommandHandlerStatus{
playGVCNextAudio()
return .success
}
设置信息
func setLockScreenWebInfo(_ info: WebVoiceModel) {
var infoDic: [String : Any] = [:]
infoDic[MPMediaItemPropertyTitle] = info.mainTitle // 歌曲名
infoDic[MPMediaItemPropertyArtist] = "" // 歌手
infoDic[MPMediaItemPropertyArtwork] = image //图片
infoDic[MPMediaItemPropertyPlaybackDuration] = // 歌曲总时长
infoDic[MPNowPlayingInfoPropertyElapsedPlaybackTime] = // 当前播放时间
// 当在控制中心暂停时,需要把播放速度设成0,不然进度条会抖动
infoDic[MPNowPlayingInfoPropertyPlaybackRate] = getMusicSpeed() // 播放速度
MPNowPlayingInfoCenter.default().nowPlayingInfo = infoDic
}
网友评论