【问题标题】:xcode - MPNowPlayingInfoCenter info is not displayed on iOS 8xcode - MPNowPlayingInfoCenter 信息不显示在 iOS 8 上
【发布时间】:2015-07-17 00:15:04
【问题描述】:

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

我使用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 中。

如何解决我的问题?我在互联网上没有找到任何东西。

在此先感谢您,费边。

【问题讨论】:

  • 你的代码真的被执行了吗?
  • 是的,[[MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo objectForKey:MPMediaItemPropertyTitle] 返回正确的 SongName。
  • 啊哈!这真是个好消息。因此,您的代码不仅在运行,而且实际上成功设置了nowPlayingInfo。所以让我问你这个 - 你是第一响应者,所以你成功地接收到来自锁屏和控制中心的远程控制事件?
  • 遥控器实际上并没有显示在锁定屏幕上,我不认为,我的应用程序成为第一响应者。
  • 我认为这就是问题所在。除非您是第一响应者,否则您无法影响锁定屏幕。

标签: ios xcode mpmovieplayercontroller lockscreen audio-player


【解决方案1】:

问题是你不满足成为锁屏和控制中心大师的要求,如I explain in my book。您应该会看到现代(iOS 8)等价物:

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

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

我不知道你省略了哪些;它可能不止一个。您可能想在此处将您的代码与工作示例进行比较:

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

【讨论】:

  • 是的,这就是解决方案。非常感谢您的帮助!
  • 不客气。要求很多啊!省略其中一个很容易,然后事情神秘地不起作用。 :(
  • 在通过 WKWebView 玩游戏时有没有运气让它工作?我可以通过 WKWebView 获得其他来源来更改正在播放的内容,而不是在播放时。
  • 这在 AVQueuePlayer 中可行吗?意味着我可以在 AVplayer 外部添加 NSMutableDictionary 以设置艺术品..等
  • 我验证了我的应用程序确实完成了列表中的所有操作,但是当我第一次启动应用程序并播放流时,锁屏控件和信息没有出现。我必须停止并重新启动流(Icecast),然后在第二次尝试时出现锁屏控件和信息。第一次尝试和第二次尝试的步骤完全相同(即没有线程或异步竞争条件)。所以我不知道为什么第一次尝试时锁定屏幕无法正常工作。
【解决方案2】:

想要扩展@matt 的答案-使用AVAudioSessionCategoryOptionMixWithOthers 选项时设置nowPlayingInfo 是没用的。

【讨论】:

  • 来这里添加这个 - 如果您添加 .mixWithOthers 密钥,您将无法按预期设置控制中心详细信息。
【解决方案3】:

进一步扩展 @matt 的答案,当应用程序返回前台 (applicationDidBecomeActive) 时,您需要调用 endReceivingRemoteControlEvents 和 resignFirstResponder。否则,操作系统会假定您是一个坏演员(或忘记关闭它们)并关闭您完全显示睡眠控件的能力,即使您再次致电 beginReceivingRemoteControlEvents 也是如此。我添加了这些调用,现在睡眠控制总是在它们应该显示的时候显示。

【讨论】:

    【解决方案4】:

    想解释@zakhej 的答案——设置 AVAudioSessionCategoryOptionMixWithOthers 时会发生什么。

    1. MPNowPlayingInfoCenter 是单例的,这意味着只能由一个应用程序设置。
    2. AVAudioSession 与其他应用共享,每个应用都可以设置。

    所以。当你设置类别AVAudioSessionCategoryAmbient,AVAudioSessionCategoryPlayAndRecord并与其他人混合,AVAudioSessionCategoryPlayback与其他人混合时,设置MPNowPlayingInfoCenter的nowPlayingInfo是没有用的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多