现在播放指示与AVPlayer播放不同步

时间:2015-06-26 19:59:40

标签: ios objective-c avplayer

我有一个现在正在播放的指示器视图,我已经制作了视频播放,但大多数情况下它正在工作,但是我注意到我的指示器并没有同步,因为它朝向播放时间结束。

视频完成并且不会像它应该的那样循环回来,因为指示符的X原点并未穿过正确的擦除器的X原点,这意味着同步不正确。

我很感激提供的任何帮助。这是在播放结束时指示器错误定位的图片。

enter image description here

- (void)observePlayerTime
{
    CMTime intervalTime = CMTimeMake(33, 1000);

    __weak typeof(self) weakSelf = self;

    self.playbackObserver = [self.playerLayer.player addPeriodicTimeObserverForInterval:
    intervalTime queue:dispatch_get_main_queue() usingBlock:^(CMTime time)
    {
        UICollectionViewFlowLayout *videoFrameLayout =
        (UICollectionViewFlowLayout *)
        weakSelf.videoFramesCollectionView.collectionViewLayout;

        if (weakSelf.playerLayer.player.rate >= 1.0)
        {
            CGRect newFrame = weakSelf.nowPlayingIndicatorView.frame;

            newFrame.origin.x += (videoFrameLayout.itemSize.width / intervalTime.value);

            if (newFrame.origin.x >= weakSelf.rightScrubberView.frame.origin.x)
            {
                // Here we loop the player

                CGRect newIndicatorFrame = weakSelf.nowPlayingIndicatorView.frame;

                newIndicatorFrame.origin.x = (weakSelf.leftScrubberView.frame.origin.x 
                    + weakSelf.leftScrubberView.bounds.size.width) 
                    - newIndicatorFrame.size.width;

                weakSelf.nowPlayingIndicatorView.frame = newIndicatorFrame;

                [weakSelf.playerLayer.player seekToTime:weakSelf.lastPlayerTime 
                    toleranceBefore:kCMTimeZero toleranceAfter:kCMTimeZero];
            }
            else
            {
                weakSelf.nowPlayingIndicatorView.frame = newFrame;
            }
        }
    }];
}

1 个答案:

答案 0 :(得分:0)

我似乎已使用dispatch_source_t代替addPeriodicTimeObserverForInterval

解决了问题

代码:

@property (nonatomic, strong) dispatch_source_t playbackTimer;

- (void)observePlaybackTime
{
    self.playbackTimer = CreateTimerDispatchSource(50000000, 0,
    dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0),^
    {
        dispatch_async(dispatch_get_main_queue(),^
        {
            if (self.playerLayer.player.rate >= 1.0)
            {
                UICollectionViewFlowLayout *videoFrameLayout = 
                (UICollectionViewFlowLayout *)
                self.videoFramesCollectionView.collectionViewLayout;

                CGRect newFrame = self.nowPlayingIndicatorView.frame;

                newFrame.origin.x += videoFrameLayout.itemSize.width / 20.7;

                if (newFrame.origin.x >= self.rightScrubberView.frame.origin.x)
                {
                    if (CMTIME_IS_VALID(self.lastPlayerTime))
                    {
                        // Loop playback

                        CGRect newIndicatorFrame = self.nowPlayingIndicatorView.frame;

                        newIndicatorFrame.origin.x =
                        self.leftScrubberView.frame.origin.x
                        + self.leftScrubberView.bounds.size.width;

                        self.nowPlayingIndicatorView.frame = newIndicatorFrame;

                        [self.playerLayer.player 
                        seekToTime:self.lastPlayerTime 
                        toleranceBefore:kCMTimeZero toleranceAfter:kCMTimeZero];
                    }
                }
                else
                {
                    self.nowPlayingIndicatorView.frame = newFrame;
                }
            }
        });
    });
}

dispatch_source_t CreateTimerDispatchSource(uint64_t interval, uint64_t leeway, 
dispatch_queue_t queue, dispatch_block_t block)
{
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,
    queue);

    if (timer)
    {
        dispatch_source_set_timer(timer, dispatch_walltime(NULL, 0), interval, leeway);

        dispatch_source_set_event_handler(timer, block);

        dispatch_resume(timer);
    }

    return timer;
}