2

我正在使用 AVPlayer 来重现音乐。在iOS 6+的模拟器上一切正常。但是当我在设备(iPhone 5s、iOS 7.1)上运行它时,MPNowPlayingInfoCenter 会显示来自任何其他音乐应用程序的信息:spotify / 声音云 / 音乐播放器,而不是我应用程序中当前正在播放的项目。

这是我用来在 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;
}

任何帮助都感激不尽。

4

1 回答 1

3

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

我正在为每首歌曲创建新的播放器,如下所示:

self.audioPlayer = [AVPlayer playerWithURL:url];

我改变了它:

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

神奇地它开始起作用了。

于 2015-02-09T22:58:09.813 回答