2015-02-09 220 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]; 

,並奇蹟般地它開始工作。

相關問題