Timeline Progress bar for AVPlayer Timeline Progress bar for AVPlayer ios ios

Timeline Progress bar for AVPlayer


Please use the below code which is from apple example code "AVPlayerDemo".

    double interval = .1f;      CMTime playerDuration = [self playerItemDuration]; // return player duration.    if (CMTIME_IS_INVALID(playerDuration))     {        return;    }     double duration = CMTimeGetSeconds(playerDuration);    if (isfinite(duration))    {        CGFloat width = CGRectGetWidth([yourSlider bounds]);        interval = 0.5f * duration / width;    }    /* Update the scrubber during normal playback. */    timeObserver = [[player addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds(interval, NSEC_PER_SEC)                                                           queue:NULL                                                      usingBlock:                                                      ^(CMTime time)                                                       {                                                          [self syncScrubber];                                                      }] retain];- (CMTime)playerItemDuration{    AVPlayerItem *thePlayerItem = [player currentItem];    if (thePlayerItem.status == AVPlayerItemStatusReadyToPlay)    {                return([playerItem duration]);    }    return(kCMTimeInvalid);}

And in syncScrubber method update the UISlider or UIProgressBar value.

- (void)syncScrubber{    CMTime playerDuration = [self playerItemDuration];    if (CMTIME_IS_INVALID(playerDuration))     {        yourSlider.minimumValue = 0.0;        return;    }     double duration = CMTimeGetSeconds(playerDuration);    if (isfinite(duration) && (duration > 0))    {        float minValue = [ yourSlider minimumValue];        float maxValue = [ yourSlider maximumValue];        double time = CMTimeGetSeconds([player currentTime]);        [yourSlider setValue:(maxValue - minValue) * time / duration + minValue];    }} 


Thanks to iOSPawan for the code!I simplified the code to the necessary lines. This might be more clear to understand the concept. Basically I have implemented it like this and it works fine.

Before starting the video:

__weak NSObject *weakSelf = self;    [_player addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds(1.0 / 60.0, NSEC_PER_SEC)                                      queue:NULL                                 usingBlock:^(CMTime time){                                                [weakSelf updateProgressBar];                                             }];[_player play];

Then you need to have a method to update your progress bar:

- (void)updateProgressBar{    double duration = CMTimeGetSeconds(_playerItem.duration);    double time = CMTimeGetSeconds(_player.currentTime);    _progressView.progress = (CGFloat) (time / duration);}


    let progressView = UIProgressView(progressViewStyle: UIProgressViewStyle.Bar)    self.view.addSubview(progressView)    progressView.constrainHeight("\(1.0/UIScreen.mainScreen().scale)")    progressView.alignLeading("", trailing: "", toView: self.view)    progressView.alignBottomEdgeWithView(self.view, predicate: "")    player.addPeriodicTimeObserverForInterval(CMTimeMakeWithSeconds(1/30.0, Int32(NSEC_PER_SEC)), queue: nil) { time in        let duration = CMTimeGetSeconds(playerItem.duration)        progressView.progress = Float((CMTimeGetSeconds(time) / duration))    }


matomo