0

I'm having trouble setting the currentTime and songLength of the MPNowPlayingInfoCenter. Here's my code:

func updateNowPlayingCenter (title: String, artist: String, albumArt: AnyObject, currentTime: NSNumber, songLength: NSNumber, PlaybackRate: Double){

    var songInfo: Dictionary <NSObject, AnyObject> = [

        MPMediaItemPropertyTitle as NSObject: title as AnyObject,

        MPMediaItemPropertyArtist as NSObject: artist as AnyObject,

        MPMediaItemPropertyArtwork as NSObject: ???,

        MPNowPlayingInfoPropertyElapsedPlaybackTime as NSObject: currentTime,

        MPMediaItemPropertyPlaybackDuration as NSObject: songLength,

        MPNowPlayingInfoPropertyPlaybackRate as NSObject: PlaybackRate as AnyObject


    ]

    MPNowPlayingInfoCenter.default().nowPlayingInfo = songInfo as [NSObject : AnyObject] as! [String : Any]

}

And here's where I set the properties in the viewWillAppear:

updateNowPlayingCenter(title: titleText[thisSong], artist: authorText[thisSong], albumArt: ??? as AnyObject, currentTime: ???, songLength: ???, PlaybackRate: 1.0)

I tried to use audioPlayer.currentTime and audioPlayer.duration, but it didn't work. How do I do this? Also, I can't figure out how to set the image of the MPMediaItemPropertyArtwork. I have the files in my assests and the image name equals the titleText[thisSong]. If you could also help me with that, that would be awesome! Thanks!

iFunnyVlogger
  • 437
  • 1
  • 6
  • 17

1 Answers1

1

Apple's Media Playback Programming Guide provides the answer:

func setupNowPlaying() {
    // Define Now Playing Info
    var nowPlayingInfo = [String : Any]()
    nowPlayingInfo[MPMediaItemPropertyTitle] = "My Movie"
    if let image = UIImage(named: "lockscreen") {
        nowPlayingInfo[MPMediaItemPropertyArtwork] =
            MPMediaItemArtwork(boundsSize: image.size) { size in
                return image
        }
    }
    nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = playerItem.currentTime().seconds
    nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = playerItem.asset.duration.seconds
    nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = player.rate

    // Set the metadata
    MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
}
CyberMoai
  • 496
  • 3
  • 11