【问题标题】:AVPlayer + MPNowPlayingInfoCenter works on simulator but not on deviceAVPlayer + MPNowPlayingInfoCenter 适用于模拟器但不适用于设备
【发布时间】:2015-02-09 20:00:31
【问题描述】:

我正在使用 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;
}

任何帮助将不胜感激。

【问题讨论】:

    标签: ios iphone avplayer mpnowplayinginfocenter


    【解决方案1】:

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

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

    self.audioPlayer = [AVPlayer playerWithURL:url];
    

    我改成了:

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

    神奇地它开始起作用了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-01
      • 2011-03-08
      • 2015-02-13
      • 2011-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多