xcode - iOS 8上不显示MPNowPlayingInfoCenter信息 [英] xcode - MPNowPlayingInfoCenter info is not displayed on iOS 8

查看:1378
本文介绍了xcode - iOS 8上不显示MPNowPlayingInfoCenter信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我使用 MPMoviePlayerController 播放音乐。我的代码启动 MPMoviePlayerController

  NSString * resourcePath = [[ NSBundle mainBundle] resourcePath]; 
resourcePath = [resourcePath stringByAppendingString:@/ music.m4a];
NSError *错误;
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中显示歌曲名称,专辑名称和专辑插图。我正在使用以下代码:

 类playingInfoCenter = NSClassFromString(@MPNowPlayingInfoCenter); 
if(playingInfoCenter){
NSMutableDictionary * songInfo = [[NSMutableDictionary alloc] init];
MPMediaItemArtwork * albumArt = [[MPMediaItemArtwork alloc] initWithImage:[UIImage imageNamed:@artwork.png]];
[songInfo setObject:@SongNameforKey:MPMediaItemPropertyTitle];
[songInfo setObject:@ArtistNameforKey:MPMediaItemPropertyArtist];
[songInfo setObject:@AlbumTitleforKey:MPMediaItemPropertyAlbumTitle];
[songInfo setObject:albumArt forKey:MPMediaItemPropertyArtwork];
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];
}

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



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



提前致谢Fabian。

解决方案

问题是你不能满足成为锁屏和控制中心主人的要求,因为


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:

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:

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.

How can I solve my problem? I didn't find anything on the internet.

Thanks in advance, Fabian.

解决方案

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:

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.

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:

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

这篇关于xcode - iOS 8上不显示MPNowPlayingInfoCenter信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆