xcode - MPNowPlayingInfoCenter 信息不显示在 iOS 8 上
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30083862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
xcode - MPNowPlayingInfoCenter info is not displayed on iOS 8
提问by FTFT1234
I'm developing a music application, which should play music in the background.
我正在开发一个音乐应用程序,它应该在后台播放音乐。
I use the MPMoviePlayerController
to play the music. My code to initiate the MPMoviePlayerController
:
我用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];
When I execute [self.player play];
the music starts.
But I also want to display the name of the song, the name of the album and the album artwork in the LockScreen and the ControlCenter. I'm using the following code:
当我执行[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];
}
But nothing gets displayed in the LockScreen. It doesn't get displayed in the ControlCenter either.
但是 LockScreen 中没有显示任何内容。它也不会显示在 ControlCenter 中。
How can I solve my problem? I didn't find anything on the internet.
我该如何解决我的问题?我没有在互联网上找到任何东西。
Thanks in advance, Fabian.
提前致谢,法比安。
回答by matt
The problem is that you are not satisfying the requirements to become master of the lock screen and control center, as I explain in my book. You should be seeing the modern (iOS 8) equivalent of this:
问题是你没有满足成为锁屏和控制中心大师的要求,正如我在我的书中解释的那样。您应该看到与此等效的现代(iOS 8):
The fact that you are not seeing it suggests that you are omitting one or more of the requirements, which I list (quoting directly from my book here):
您没有看到它的事实表明您忽略了我列出的一个或多个要求(直接从我的书中引用):
- Your app must contain a UIResponder in its responder chain that
returns YES from
canBecomeFirstResponder
, and that responder must actually be first responder. - Some UIResponder in the responder chain,
at or above the first responder, must implement
remoteControlReceivedWithEvent:
. - Your app must call the UIApplication
instance method
beginReceivingRemoteControlEvents
. - Your app's audio session's policy must be Playback.
- Your app must be emitting some sound.
- 您的应用程序必须在其响应者链中包含一个 UIResponder,它从 返回 YES
canBecomeFirstResponder
,并且该响应者实际上必须是第一响应者。 - 响应者链中的某些 UIResponder,在第一响应者或第一响应者之上,必须实现
remoteControlReceivedWithEvent:
. - 您的应用程序必须调用 UIApplication 实例方法
beginReceivingRemoteControlEvents
。 - 您的应用的音频会话策略必须是播放。
- 您的应用程序必须发出一些声音。
I don't know which of those you are omitting; it could be more than one. You might like to compare your code with a working example, here:
我不知道你忽略了哪些;它可能不止一个。您可能想将您的代码与一个工作示例进行比较,如下所示:
回答by Artem Shmatkov
Want to expand @matt's answer - setting nowPlayingInfo is useless when you use AVAudioSessionCategoryOptionMixWithOthers
option.
想要扩展@matt 的答案 - 使用AVAudioSessionCategoryOptionMixWithOthers
选项时设置 nowPlayingInfo 是无用的。
回答by Tyler Sheaffer
Expanding further on @matt's answer, it's required that you call endReceivingRemoteControlEvents
and resignFirstResponder
when the app comes back into the foreground (applicationDidBecomeActive
). Otherwise the OS assumes you are a bad actor (or forgot to turn them off) and turns off your ability to show the sleep controls entirely, even after you call beginReceivingRemoteControlEvents
again. I added these calls and now the Sleep Controls always show when they should.
进一步扩展@matt 的答案,需要您调用endReceivingRemoteControlEvents
并resignFirstResponder
在应用程序返回前台时 ( applicationDidBecomeActive
)。否则操作系统会假设你是一个坏演员(或忘记关闭它们)并关闭你完全显示睡眠控制的能力,即使你beginReceivingRemoteControlEvents
再次打电话。我添加了这些调用,现在睡眠控制总是在它们应该显示的时候显示。
回答by zhonglaoban
Want to explain @zakhej's answer - what's happens when set AVAudioSessionCategoryOptionMixWithOthers.
想解释@zakhej 的答案 - 设置 AVAudioSessionCategoryOptionMixWithOthers 时会发生什么。
- MPNowPlayingInfoCenter is a singleton, which means can only one application set it.
- AVAudioSession is shared with other apps, every app can set it.
- MPNowPlayingInfoCenter 是一个单例,这意味着只能由一个应用程序设置它。
- AVAudioSession 与其他应用程序共享,每个应用程序都可以设置它。
so. when you set category AVAudioSessionCategoryAmbient, AVAudioSessionCategoryPlayAndRecord and mix with others, AVAudioSessionCategoryPlayback and mix with others, setting MPNowPlayingInfoCenter's nowPlayingInfo is useless.
所以。当您设置类别 AVAudioSessionCategoryAmbient、AVAudioSessionCategoryPlayAndRecord 并与其他人混合、AVAudioSessionCategoryPlayback 并与其他人混合时,设置 MPNowPlayingInfoCenter 的 nowPlayingInfo 是无用的。