【问题标题】:AVQueuePlayer AVPlayerItemDidPlayToEndTimeNotification fails to callAVQueuePlayer AVPlayerItemDidPlayToEndTimeNotification 调用失败
【发布时间】:2013-12-16 12:49:50
【问题描述】:

我使用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);

}

问题是,该方法只为播放的第一个视频调用,之后,该方法停止被调用,所以如果我在数组中有两部电影,它会同时播放它们+第一个再次,知道为什么会这样吗?

更多信息: 还尝试每次都制作一个新播放器并将其设置为分层。同一条消息多次发送失败

- (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];

}

【问题讨论】:

    标签: ios objective-c ios7 avplayer avqueueplayer


    【解决方案1】:

    经过很多混乱,显然出于某种原因,视图每次都取消注册为观察者,我只是在每次通知后删除并添加观察者:

    - (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];
    
    }
    

    【讨论】:

    • 因为您拒绝通知 AVPlayerItem,并且您正在使用数组初始化 AVQueuePlayer,因此您需要为该数组中的每个 playerItem 注册 AVPlayerItemDidPlayToEndNotification,而不仅仅是 currentItem
    【解决方案2】:

    寻求解决此问题的干净方法。我改为使用下一段代码

    首先,您必须添加必要的代码,以便在再现时间更改时接收来自 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];
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-24
      • 1970-01-01
      • 2021-04-20
      • 2014-05-29
      • 2021-12-23
      • 2019-08-15
      • 2020-05-27
      相关资源
      最近更新 更多