xcode – MPNowPlayingInfoCenter信息不显示在iOS 8上

我正在开发一个音乐应用程序,它应该在后台播放音乐。

我使用MPMoviePlayerController播放音乐。 我的代码来启动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]; 

当我执行[self.player play]; 音乐开始。 但是我也想要在LockScreen和ControlCenter中显示歌曲名称,专辑名称和专辑封面。 我使用下面的代码:

 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]; } 

但LockScreen中没有任何显示。 它也不会显示在ControlCenter中。

我该如何解决我的问题? 我在网上找不到任何东西。

在此先感谢,Fabian。

问题在于你不能满足成为锁屏和控制中心的要求,正如我在书中所解释的那样 。 你应该看到现代(iOS 8)相当于这个:

在这里输入图像说明

你没有看到它的事实表明你正在省略一个或多个要求,我列出(直接从我的书在这里引用):

  • 您的应用程序必须在响应者链中包含一个UIResponder,该响应者链从canBecomeFirstResponder返回YES,并且该响应者实际上必须是第一个响应者。
  • 响应者链中的一些UIResponder,在第一个响应者的上面,必须实现remoteControlReceivedWithEvent:
  • 您的应用程序必须调用UIApplication实例方法beginReceivingRemoteControlEvents
  • 您的应用的audio会话的政策必须是回放。
  • 你的应用程序必须发出一些声音。

我不知道你在省略哪一个, 它可能不止一个。 你可能想比较你的代码和一个工作的例子,在这里:

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

想要展开@ matt的答案 – 设置nowPlayingInfo在使用AVAudioSessionCategoryOptionMixWithOthers选项时是无用的。

在@ matt的回答中进一步扩展,需要在应用程序回到前台时调用endReceivingRemoteControlEventsresignFirstResponderapplicationDidBecomeActive )。 否则,操作系统认为你是一个糟糕的演员(或忘记closures他们),并closures了你完全显示睡眠控制的能力,即使你再次调用beginReceivingRemoteControlEvents 。 我添加了这些电话,现在睡眠控制器总是显示他们应该什么时候。