开发者

iOS AVPlayer seektotime locking at certain points

I'm more or less using the code from here: AVPlayer Video SeekToTime However, when I try to scroll it seems to lock to certain time points (basically every frame that lays on the second time mark), so when I scrub through the scrubber keeps shooting back and forth between where my finger is and the last second it passed and the video only changes at those second marks.

Now one "big" change I did make beca开发者_如何学Gouse we want smooth scrolling is that anywhere there is a "seekToTime" I replaced it with a seekToTime:toleranceBefore:kCMTimeZero toleranceAfter:kCMTimeZero.

If you need any more info please let me know! Thanks in advance!


refer below code: this code is some part of Apple Sample code

AVPlayerDemo

If you try to implement a streaming player, refer to the sample code below.

StitchedStreamPlayer

also, a complete implements to exactly scrub is to using the like slider below. because great Video Player is should be considering UX. as you know running default Video App. as you scrubbing, if drag down slider must be fine tuning.

CPSlider | OBSlider

/* The user is dragging the movie controller thumb to scrub through the movie. */
- (IBAction)beginScrubbing:(id)sender
{
    mRestoreAfterScrubbingRate = [mPlayer rate];
    [mPlayer setRate:0.f];

    /* Remove previous timer. */
    [self removePlayerTimeObserver];
}

/* Set the player current time to match the scrubber position. */
- (IBAction)scrub:(id)sender
{
    if ([sender isKindOfClass:[UISlider class]])
    {
        UISlider* slider = sender;

        CMTime playerDuration = [self playerItemDuration];
        if (CMTIME_IS_INVALID(playerDuration)) {
            return;
        } 

        double duration = CMTimeGetSeconds(playerDuration);
        if (isfinite(duration))
        {
            float minValue = [slider minimumValue];
            float maxValue = [slider maximumValue];
            float value = [slider value];

            double time = duration * (value - minValue) / (maxValue - minValue);

            [mPlayer seekToTime:CMTimeMakeWithSeconds(time, NSEC_PER_SEC)];
        }
    }
}

/* The user has released the movie thumb control to stop scrubbing through the movie. */
- (IBAction)endScrubbing:(id)sender
{
    if (!mTimeObserver)
    {
        CMTime playerDuration = [self playerItemDuration];
        if (CMTIME_IS_INVALID(playerDuration)) 
        {
            return;
        } 

        double duration = CMTimeGetSeconds(playerDuration);
        if (isfinite(duration))
        {
            CGFloat width = CGRectGetWidth([mScrubber bounds]);
            double tolerance = 0.5f * duration / width;

            mTimeObserver = [[mPlayer addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds(tolerance, NSEC_PER_SEC) queue:NULL usingBlock:
            ^(CMTime time)
            {
                [self syncScrubber];
            }] retain];
        }
    }

    if (mRestoreAfterScrubbingRate)
    {
        [mPlayer setRate:mRestoreAfterScrubbingRate];
        mRestoreAfterScrubbingRate = 0.f;
    }
}


Along with bitmapdata.com answer (using [AVPlayer setRate] instead of pause/seekTime) you need to have a video encoded with a keyframe on every frames. Just re-encoded my video with that setting and now I can navigate through my video very smoothly!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜