为 MPMediaItemArtwork 声明变量(init() 不可用)
Declaring Variable for MPMediaItemArtwork (init() is unavailable)
我正在为 Control Center 创建专辑封面:
func updateNowPlayingCenter (title: String, artist: String, PlaybackRate: Double){
var artwork = MPMediaItemArtwork()
let image:UIImage = UIImage(named: title)!
if #available(iOS 10.0, *) {
artwork = MPMediaItemArtwork.init(boundsSize: image.size, requestHandler: { (size) -> UIImage in
return image
})
} else {
artwork = MPMediaItemArtwork(image: image)
}
var songInfo: Dictionary <NSObject, AnyObject> = [
MPMediaItemPropertyTitle as NSObject: title as AnyObject,
MPMediaItemPropertyArtist as NSObject: artist as AnyObject,
MPMediaItemPropertyArtwork as NSObject: artwork as AnyObject,
MPMediaItemPropertyPlaybackDuration as NSObject: audioPlayer.duration as AnyObject,
MPNowPlayingInfoPropertyElapsedPlaybackTime as NSObject: audioPlayer.currentTime as AnyObject,
MPNowPlayingInfoPropertyPlaybackRate as NSObject: PlaybackRate as AnyObject
]
MPNowPlayingInfoCenter.default().nowPlayingInfo = songInfo as [NSObject : AnyObject] as! [String : Any]
}
但是我得到了错误
init() is unavailable
上线artwork = MPMediaItemArtwork()
线。那么,如何为 artwork
声明一个变量,以便我可以在行 MPMediaItemPropertyArtwork as NSObject: artwork as AnyObject,
上调用它?
好的,修复非常简单。我刚换了
if #available(iOS 10.0, *) {
artwork = MPMediaItemArtwork.init(boundsSize: image.size, requestHandler: { (size) -> UIImage in
return image
})
} else {
artwork = MPMediaItemArtwork(image: image)
}
和
let artwork = MPMediaItemArtwork(image: image)
我正在为 Control Center 创建专辑封面:
func updateNowPlayingCenter (title: String, artist: String, PlaybackRate: Double){
var artwork = MPMediaItemArtwork()
let image:UIImage = UIImage(named: title)!
if #available(iOS 10.0, *) {
artwork = MPMediaItemArtwork.init(boundsSize: image.size, requestHandler: { (size) -> UIImage in
return image
})
} else {
artwork = MPMediaItemArtwork(image: image)
}
var songInfo: Dictionary <NSObject, AnyObject> = [
MPMediaItemPropertyTitle as NSObject: title as AnyObject,
MPMediaItemPropertyArtist as NSObject: artist as AnyObject,
MPMediaItemPropertyArtwork as NSObject: artwork as AnyObject,
MPMediaItemPropertyPlaybackDuration as NSObject: audioPlayer.duration as AnyObject,
MPNowPlayingInfoPropertyElapsedPlaybackTime as NSObject: audioPlayer.currentTime as AnyObject,
MPNowPlayingInfoPropertyPlaybackRate as NSObject: PlaybackRate as AnyObject
]
MPNowPlayingInfoCenter.default().nowPlayingInfo = songInfo as [NSObject : AnyObject] as! [String : Any]
}
但是我得到了错误
init() is unavailable
上线artwork = MPMediaItemArtwork()
线。那么,如何为 artwork
声明一个变量,以便我可以在行 MPMediaItemPropertyArtwork as NSObject: artwork as AnyObject,
上调用它?
好的,修复非常简单。我刚换了
if #available(iOS 10.0, *) {
artwork = MPMediaItemArtwork.init(boundsSize: image.size, requestHandler: { (size) -> UIImage in
return image
})
} else {
artwork = MPMediaItemArtwork(image: image)
}
和
let artwork = MPMediaItemArtwork(image: image)