I'm working an iOS application that uses an AVPlayer to play songs represented by AVPlayerItems.
I've tried setting the nowPlayingInfo property of MPNowPlayingInfoCenter.defaultCenter() but it appears that the info gets immediately overwritten by the AVPlayer since it needs to update other info like elapsedTime and playbackDuration.
Even immediately after the program executes
MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo?[MPMediaItemPropertyTitle] = "Title"
The printout of MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo
is
Info: ["MPNowPlayingInfoPropertyElapsedPlaybackTime": 34.89555007300078, "AVNowPlayingInfoControllerIdentifierKey": <__NSConcreteUUID 0x13778e9b0> 9141DD1C-FD09-4210-B3C7-B948522E3AF6, "playbackDuration": 198.2516666666667, "MPNowPlayingInfoPropertyPlaybackRate": 1]
What am I doing wrong? Is it possible to store additional metadata like title/album name in an AVPlayerItem so it gets displayed on the info center?
Also, what is AVNowPlayingInfoControllerIdentifierKey? There do not seem to be any classes named AVNowPlayingInfoController.
The problem is that this line does nothing:
MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo?[MPMediaItemPropertyTitle] =
"Title"
Even if that line actually did anything, you would be fetching a copy of the nowPlayingInfo
as a separate dictionary and then writing into that separate dictionary. You would not be writing into the actual now playing info, which is what you want to do.
The correct approach is to form a dictionary and then set MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo
to that dictionary.
For example:
MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo =
[MPMediaItemPropertyTitle : "Title"]
(Of course the dictionary can have more entries.)