0

AVQueuePlayer用来循环遍历数组AVPlayerItems。我循环它的方式,我听它,AVPlayerItemDidPlayToEndTimeNotification每次调用它时,我都会将当前对象添加到队列的末尾。继承人的代码:

    viewWillAppear
{
    ...

    [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(playerItemDidReachEnd:)
                                                     name:AVPlayerItemDidPlayToEndTimeNotification
                                                   object:[_queuePlayer currentItem]];

        [_queuePlayer play];
}
- (void)playerItemDidReachEnd:(NSNotification *)notification {
    AVPlayerItem *p = [notification object];
    AVPlayerItem *fakeNew = [[AVPlayerItem alloc] initWithAsset:p.asset];
    if (_queuePlayer.items.count == 1)
    {
        [p seekToTime:kCMTimeZero];
        [_queuePlayer play];
    }
    else
    {
        [_queuePlayer insertItem:fakeNew afterItem:[_queuePlayer.items lastObject]];
    }
    NSLog(@"array of items to play:%lu", (unsigned long)_queuePlayer.items.count);

}

问题是,该方法仅针对播放的第一个视频调用,之后,该方法停止被调用,因此,例如,如果我在数组中有 2 部电影,它将再次播放它们+第一个,任何想法为什么会这样?

更多信息:还尝试每次都制作一个新播放器并将其设置为图层。未能多次发送通知

- (void)playerItemDidReachEnd:(NSNotification *)notification {
    AVPlayerItem *p = [notification object];
    [self.playList removeObjectAtIndex:0];
    [self.playList addObject:p];
    AVPlayer *newPlayer = [[AVPlayer alloc] initWithPlayerItem:[self.playList objectAtIndex:0]];
    _player = newPlayer;
    self.AVPlayerLayerView.layer.player = self.player;
    [_player play];

}
4

2 回答 2

1

After a lot of messing around, apparently for whatever reason, the view unregistered as observer every time, I just removed and added observer after every notification:

- (void)playerItemDidReachEnd:(NSNotification *)notification {
    AVPlayerItem *p = [notification object];
    AVPlayerItem *fakeNewItem = [[AVPlayerItem alloc] initWithAsset:p.asset];
    [self.playList removeObjectAtIndex:0];
    [self.playList addObject:fakeNewItem];
    AVPlayer *newPlayer = [[AVPlayer alloc] initWithPlayerItem:[self.playList objectAtIndex:0]];
    _player = newPlayer;
    self.AVPlayerLayerView.layer.player = self.player;
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(playerItemDidReachEnd:)
                                                 name:AVPlayerItemDidPlayToEndTimeNotification
                                               object:[_player currentItem]];
    [_player play];

}
于 2013-12-18T08:36:13.807 回答
0

一个干净的方法来解决这个问题。我改为使用下一段代码

首先是您必须添加必要的代码,以便在再现时间更改时接收来自 AVPlayer 的反馈。

- (void)addPeriodicTimeObserverForReproductionChanges {
    @weakify(self);
    [self.player 
    addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds(kBQDefaultTimeIntervalReproductionChanges, NSEC_PER_SEC) 
    queue:self.eventsQueue
    usingBlock:^(CMTime time) {
    @strongify(self);
    [self dispatchBlockOnMainQueue:^{
        if ([self.delegate respondsToSelector:@selector(playerItemController:didChangeReproductionTime:)])
             [self.delegate playerItemController:self
                       didChangeReproductionTime:time];

         [self checkForItemPlayToEnd];
     }]; 
 }];

}

- (void)checkForItemPlayToEnd 
{
    CMTime durationScaled = CMTimeConvertScale(self.duration,self.player.currentTime.timescale, kCMTimeRoundingMethod_Default);

    if (CMTIME_COMPARE_INLINE(durationScaled, ==, self.player.currentTime)) {
        [self playerDidFinishReproducingItem];
    }
}
于 2014-07-18T12:46:06.807 回答