2017-07-26 71 views
0

我創建的專輯封面的控制中心這樣的:變量聲明爲MPMediaItemArtwork(的init()是不可用)

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()是不可用

上線artwork = MPMediaItemArtwork()行。那麼,如何爲artwork聲明一個變量,以便我可以在線MPMediaItemPropertyArtwork as NSObject: artwork as AnyObject,上調用它?

回答

0

好了,很簡單的修補程序。我剛剛更換

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) 

這是它大聲笑。