xcode - iOS 8上不显示MPNowPlayingInfoCenter信息

FTF*_*234 6 xcode mpmovieplayercontroller audio-player lockscreen ios

我正在开发一个应该在后台播放音乐的音乐应用程序.

我用它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];
Run Code Online (Sandbox Code Playgroud)

当我执行[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];
    }
Run Code Online (Sandbox Code Playgroud)

但LockScreen中没有显示任何内容.它也不会显示在ControlCenter中.

我怎样才能解决我的问题?我没有在互联网上找到任何东西.

在此先感谢Fabian.

mat*_*att 27

问题是你不满足成为锁屏和控制中心主人的要求,正如我在书中解释的那样.您应该看到现代(iOS 8)相当于此:

在此输入图像描述

您没有看到它的事实表明您省略了我列出的一个或多个要求(直接在我的书中引用):

  • 您的应用程序必须在其响应程序链中包含一个UIResponder,它返回YES canBecomeFirstResponder,并且响应者实际上必须是第一响应者.
  • 响应者链中的某个UIResponder,在第一响应者或其上方,必须实现 remoteControlReceivedWithEvent:.
  • 您的应用必须调用UIApplication实例方法beginReceivingRemoteControlEvents.
  • 您应用的音频会话策略必须是播放.
  • 你的应用必须发出一些声音.

我不知道你忽略了哪一个; 它可能不止一个.您可能希望将代码与工作示例进行比较,此处:

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


Art*_*kov 10

想扩展@ matt的答案 - 设置nowPlayingInfo在使用AVAudioSessionCategoryOptionMixWithOthers选项时没用.