Xcode - MPNowPlayingInfoCenter information not displayed on iOS 8

I am developing a music application that should play music in the background.

I am using MPMoviePlayerController to play music. My code to run MPMoviePlayerController :

 NSString* resourcePath = [[NSBundle mainBundle] resourcePath]; resourcePath = [resourcePath stringByAppendingString:@"/music.m4a"]; NSError* err; self.player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:resourcePath]]; if (err) { NSLog(@"ERROR: %@", err.localizedDescription); } AVAudioSession *session = [AVAudioSession sharedInstance]; [session setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil]; [session setActive:YES error:nil]; [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; [self.player setShouldAutoplay:NO]; [self.player setControlStyle: MPMovieControlStyleEmbedded]; self.player.view.hidden = YES; [self.player prepareToPlay]; 

When I execute [self.player play]; , the music begins. But I also want to show the song title, album name and album art in LockScreen and ControlCenter. I am using the following code:

 Class playingInfoCenter = NSClassFromString(@"MPNowPlayingInfoCenter"); if (playingInfoCenter) { NSMutableDictionary *songInfo = [[NSMutableDictionary alloc] init]; MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithImage: [UIImage imageNamed:@"artwork.png"]]; [songInfo setObject:@"SongName" forKey:MPMediaItemPropertyTitle]; [songInfo setObject:@"ArtistName" forKey:MPMediaItemPropertyArtist]; [songInfo setObject:@"AlbumTitle" forKey:MPMediaItemPropertyAlbumTitle]; [songInfo setObject:albumArt forKey:MPMediaItemPropertyArtwork]; [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo]; } 

But nothing is displayed in LockScreen. It also does not appear in ControlCenter.

How can I solve my problem? I did not find anything on the Internet.

Thanks Fabian.

+5
source share
3 answers

The problem is that you do not meet the requirements to become a master of the lock screen and the control center, as I explain in my book . You should see the modern (iOS 8) equivalent of this:

enter image description here

The fact that you do not see it indicates that you omit one or more of the requirements that I list (citing directly from my book here):

  • Your application must contain a UIResponder in the responder chain, returns YES from canBecomeFirstResponder , and that responder should actually be the first responder.
  • Some UIResponder in the responder chain, at or above the first responder, must implement remoteControlReceivedWithEvent:
  • Your application should call the UIApplication instance method of beginReceivingRemoteControlEvents .
  • Audio application session policy should be playback.
  • Your application should make a sound.

I do not know which one you are omitting; it may be several. You can compare your code with a working example, here:

https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/iOS7bookExamples/bk2ch14p653backgroundPlayerAndInterrupter/backgroundPlayer/backgroundPlayer/ViewController.m

+26
source

Want to expand @matt's answer - setting nowPlayingInfo is useless if you use the AVAudioSessionCategoryOptionMixWithOthers option.

+5
source

Next, with @matt's answer, you need to call endReceivingRemoteControlEvents and resignFirstResponder when the application returns to the foreground ( applicationDidBecomeActive ). Otherwise, the OS assumes that you are a bad actor (or forgot to turn them off) and disable your ability to fully display sleep controls, even after you call beginReceivingRemoteControlEvents again. I added these calls, and now Sleep Controls always show when they should.

0
source

All Articles