ios swift 锁屏开发
2022-01-14 本文已影响0人
荔枝lizhi_iOS程序猿
func nowplaying(artist: String, song: String, artworkImage: String) {
//获取锁屏中心
let playCenter = MPNowPlayingInfoCenter.default()
let infoDict = NSMutableDictionary()
//设置歌曲名
// infoDict[MPMediaItemPropertyAlbumTitle] = song
infoDict[MPMediaItemPropertyTitle] = song
//设置歌手名
infoDict[MPMediaItemPropertyArtist] = artist
// 专辑作者
// infoDict[MPMediaItemPropertyAlbumArtist] = "好嗨"
//播放速率
infoDict[MPNowPlayingInfoPropertyPlaybackRate] = self.rate
//设置图片
var showImage = UIImage(named: "play_play")!
if let image = try? UIImage(url: URL(string: artworkImage) ?? URL(fileURLWithPath: "")) {
showImage = image
}
let artWork = MPMediaItemArtwork(boundsSize: showImage.size) { size in
return showImage
}
infoDict[MPMediaItemPropertyArtwork] = artWork
let duration = self.player.currentItem?.duration
let durationTime = TimeInterval(duration?.seconds ?? 0)
//设置总时长
infoDict[MPMediaItemPropertyPlaybackDuration] = durationTime
playCenter.nowPlayingInfo = infoDict as? [String: Any]
//开启远程控制
UIApplication.shared.beginReceivingRemoteControlEvents()
CCPlayViewController.shared.becomeFirstResponder()
}