首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MPNowPlayingInfoCenter defaultCenter不会更新或检索信息

MPNowPlayingInfoCenter defaultCenter不会更新或检索信息
EN

Stack Overflow用户
提问于 2012-03-02 03:24:50
回答 2查看 17.3K关注 0票数 28

我正在更新MPNowPlayingInfoCenter,但遇到了一些麻烦。我试了相当多,到了不知所措的地步。以下是我的代码:

代码语言:javascript
复制
    self.audioPlayer.allowsAirPlay = NO;

    Class playingInfoCenter = NSClassFromString(@"MPNowPlayingInfoCenter");

    if (playingInfoCenter) {

        NSMutableDictionary *songInfo = [[NSMutableDictionary alloc] init];

        MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithImage:[UIImage imageNamed:@"series_placeholder"]];

        [songInfo setObject:thePodcast.title forKey:MPMediaItemPropertyTitle];
        [songInfo setObject:thePodcast.author forKey:MPMediaItemPropertyArtist];
        [songInfo setObject:@"NCC" forKey:MPMediaItemPropertyAlbumTitle];
        [songInfo setObject:albumArt forKey:MPMediaItemPropertyArtwork];

        [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];


    }

这不管用,我也试过了:

代码语言:javascript
复制
   [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:nil];

试图让它从iPod应用程序(或任何可能有信息的应用程序)中删除现有信息。此外,为了看看我是否能找出问题所在,我尝试在应用程序启动时检索当前信息:

代码语言:javascript
复制
  NSDictionary *info = [[MPNowPlayingInfoCenter defaultCenter] nowPlayingInfo];
  NSString *title = [info valueForKey:MPMediaItemPropertyTitle];
  NSString *author = [info valueForKey:MPMediaItemPropertyArtist];

  NSLog(@"Currently playing: %@ // %@", title, author);

我得到了Currently playing: (null) // (null)

我已经对此进行了相当多的研究,下面的文章对它进行了相当彻底的解释,然而,我仍然无法让它正常工作。我是不是遗漏了什么?会有什么干扰吗?这是我的应用程序需要注册才能访问的服务吗(在任何文档中都没有看到)?

Apple's Docs

Change lock screen background audio controls

Now playing info ignored

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-03-03 00:47:47

我终于找到了问题所在,我没有提示我的应用程序接收远程控制事件,只需添加下面这行就解决了这个问题:

代码语言:javascript
复制
 [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
票数 55
EN

Stack Overflow用户

发布于 2012-03-02 03:59:04

我使用下面的代码,它总是有效的。我也像你一样使用MPMoviePlayer。你检查过NSClassFromString(@"MPNowPlayingInfoCenter")是否真的返回过YES吗?你有没有设置你的应用程序播放音频在背景键你的plist?

代码语言:javascript
复制
- (void) loadMPInformation
{
    NSDictionary *mpInfo;

    if([savedTrack.belongingAlbum.hasAlbumArt boolValue] == NO){
        mpInfo = [NSDictionary dictionaryWithObjectsAndKeys:savedTrack.belongingAlbum.album, MPMediaItemPropertyAlbumTitle, 
                  savedTrack.belongingArtist.artist, MPMediaItemPropertyArtist, savedTrack.name, MPMediaItemPropertyTitle, nil];   
    } else {
        UIImage *artImage = [UIImage imageWithData:savedTrack.belongingAlbum.art];
        MPMediaItemArtwork *artwork = [[MPMediaItemArtwork alloc] initWithImage:artImage];
        mpInfo = [NSDictionary dictionaryWithObjectsAndKeys:savedTrack.belongingAlbum.album, MPMediaItemPropertyAlbumTitle, 
                  savedTrack.belongingArtist.artist, MPMediaItemPropertyArtist, savedTrack.name, MPMediaItemPropertyTitle, artwork, MPMediaItemPropertyArtwork, nil];
    }
    [MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = mpInfo;

}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9522591

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档