0

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.

Andrew
  • 101
  • 1
  • 10

1 Answers1

2

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.)

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Are you sure my line of code does nothing? I can see the title pop up briefly before it disappears on the now playing info screen. – Andrew Nov 29 '15 at 01:29
  • Using `MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo = [MPMediaItemPropertyTitle : "Title"]` would overwrite other data already in nowPlayingInfo provided by the AVPlayer that is required to show how long the song has been playing. I would like to add additional info to the current dictionary. – Andrew Nov 29 '15 at 01:35
  • How did "MPNowPlayingInfoPropertyElapsedPlaybackTime" get automatically set then? I'm not using MPMediaPlayer. – Andrew Nov 29 '15 at 20:08
  • @matt Same issue with me too, by default the below 3 properties are being set : **MPNowPlayingInfoPropertyElapsedPlaybackTime**, **MPNowPlayingInfoPropertyPlaybackRate**, **title** Even if I have set the NowPlayinfo using the dictionary, it still overwrites with the AVPlayer data when I PLAY n PAUSE from the control center. Can anyone just help me with this ? – Nishan29 Feb 22 '17 at 06:07
  • @Andrew : Did you get any solution for overwriting of the AVPlayer data ? – Nishan29 Feb 22 '17 at 06:08
  • I'm doing the below code : **[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil]; [[AVAudioSession sharedInstance] setActive:YES error:nil]; [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];** I'm using WKWebView to play audio/video and am looking to set custom MPNowPlayingInfoCenter nowPlayingInfo – Nishan29 Feb 22 '17 at 06:30
  • Its not about that, its about AVAudioSession trying to overwrite my data. I'm not sure by default the title is being set. – Nishan29 Feb 22 '17 at 09:30
  • MixWithOthers causes now playing not to work – tmm1 Oct 12 '22 at 22:33