swiftuiimagempmediaitem

Declaring Variable for MPMediaItemArtwork (init() is unavailable)


I'm creating an album art for Control Center like this:

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]

    }

But I get the error

init() is unavailable

on the line artwork = MPMediaItemArtwork() line. So, how do I declare a variable for artwork so that I can call it on line MPMediaItemPropertyArtwork as NSObject: artwork as AnyObject,?


Solution

  • Ok, pretty simple fix. I just replaced

    if #available(iOS 10.0, *) {
                artwork = MPMediaItemArtwork.init(boundsSize: image.size, requestHandler: { (size) -> UIImage in
                    return image
                })
            } else {
                artwork = MPMediaItemArtwork(image: image)
            }
    

    with

    let artwork = MPMediaItemArtwork(image: image)