2015-02-09 152 views
1

我正在使用AVPlayer重现音乐。所有工作只需在模拟器与iOS 6+。但是当我在设备上运行它时(iPhone 5s,iOS 7.1),MPNowPlayingInfoCenter显示来自任何其他音乐应用程序的信息:spotify/sound cloud/music player,而不是我应用程序中的当前播放项目。AVPlayer + MPNowPlayingInfoCenter在模拟器上工作,但不在设备上

这里是我用来设置音乐信息在MPNowPlayingInfoCenter代码:

- (void)setupNowPlayingInfoCenter 
{ 
    if (NSClassFromString(@"MPNowPlayingInfoCenter")) { 
     // Here enters fine. 
     MPNowPlayingInfoCenter *playingInfoCenter = [MPNowPlayingInfoCenter defaultCenter]; 

     if (self.playingUrl) { 
      NSMutableDictionary *songInfo = self.songInfo; 

      MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithImage:[self.playingObject valueForKey:@"image"]]; 

      [songInfo setObject:[self.playingObject valueForKey:@"song"] forKey:MPMediaItemPropertyTitle]; 
      [songInfo setObject:[self.playingObject valueForKey:@"artist"] forKey:MPMediaItemPropertyArtist]; 
      [songInfo setObject:[self.playingObject valueForKey:@"album"] forKey:MPMediaItemPropertyAlbumTitle]; 
      [songInfo setObject:[NSNumber numberWithDouble:self.progress] forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime]; 
      [songInfo setObject:[NSNumber numberWithDouble:self.duration] forKey:MPMediaItemPropertyPlaybackDuration]; 
      [songInfo setObject:(self.isPaused ? @(0.0f) : @(1.0f)) forKey:MPNowPlayingInfoPropertyPlaybackRate]; 
      [songInfo setObject:albumArt forKey:MPMediaItemPropertyArtwork]; 

      playingInfoCenter.nowPlayingInfo = songInfo; 
} 

其他相关代码:

- (NSError *)initialize 
{ 
    NSError *error = nil; 
    AVAudioSession *audioSession = [AVAudioSession sharedInstance]; 
    [audioSession setCategory:AVAudioSessionCategoryPlayback 
         error:&error]; 
    if (error) { 
     NSLog(@"%@", [error description]); 
     return error; 
    } 
    [audioSession setActive:YES error:&error]; 
    if (error) { 
     NSLog(@"%@", [_error description]); 
     return error; 
    } 

    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; 
    return error; 
} 

任何帮助将非常感激。

回答

2

好的,我自己找到了答案。

我创建新的播放每首歌曲,像这样:

self.audioPlayer = [AVPlayer playerWithURL:url]; 

我改变了它:

AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url]; 
[self.audioPlayer replaceCurrentItemWithPlayerItem:playerItem]; 

,并奇迹般地它开始工作。

相关问题