Create a thumbnail image for video at a current frame while sliding the player

348 views Asked by At

What I'm trying to do is to get the current frame of thumbnail image while sliding video. I expect this line could do the task:

[self.controlView.videoSlider setImage:image ? : ZFPlayerImage(@"ZFPlayer_loading_bgView")];

But I never get right about my image so always end up with ZFPlayer_loading_bgView

From what I've done some research, someone says there's something to do with CMTime dragedCMTime = CMTimeMake(dragedSeconds, 1); and I tried tinkering the latter number but nothing works! Could anyone tell me what I've been missing or how to figure this out?

Here below the relevant codes:

- (void)progressSliderValueChanged:(ASValueTrackingSlider *)slider
{
    if (self.player.currentItem.status == AVPlayerItemStatusReadyToPlay) {
        NSString *style = @"";
        CGFloat value   = slider.value - self.sliderLastValue;
        if (value > 0) { style = @">>"; }
        if (value < 0) { style = @"<<"; }
        if (value == 0) { return; }

        self.sliderLastValue    = slider.value;

        [self pause];

        CGFloat total           = (CGFloat)_playerItem.duration.value / _playerItem.duration.timescale;

        NSInteger dragedSeconds = floorf(total * slider.value);

        CMTime dragedCMTime     = CMTimeMake(dragedSeconds, 1); 

        NSInteger proMin        = (NSInteger)CMTimeGetSeconds(dragedCMTime) / 60;
        NSInteger proSec        = (NSInteger)CMTimeGetSeconds(dragedCMTime) % 60;

        NSInteger durMin        = (NSInteger)total / 60;
        NSInteger durSec        = (NSInteger)total % 60;

        NSString *currentTime   = [NSString stringWithFormat:@"%02zd:%02zd", proMin, proSec];
        NSString *totalTime     = [NSString stringWithFormat:@"%02zd:%02zd", durMin, durSec];

        if (total > 0) {
            self.controlView.videoSlider.popUpView.hidden = !self.isFullScreen;
            self.controlView.currentTimeLabel.text  = currentTime;
            if (self.isFullScreen) {
                [self.controlView.videoSlider setText:currentTime];
                dispatch_queue_t queue = dispatch_queue_create("com.playerPic.queue", DISPATCH_QUEUE_CONCURRENT);
                dispatch_async(queue, ^{
                    NSError *error;
                    CMTime actualTime;

                    CGImageRef cgImage = [self.imageGenerator copyCGImageAtTime:dragedCMTime actualTime:&actualTime error:&error];

                    CMTimeShow(actualTime);

                    UIImage *image = [UIImage imageWithCGImage:cgImage];

                    CGImageRelease(cgImage);

                    dispatch_async(dispatch_get_main_queue(), ^{
                        [self.controlView.videoSlider setImage:image ? : ZFPlayerImage(@"ZFPlayer_loading_bgView")];
                    });
                });

            } else {
                self.controlView.horizontalLabel.hidden = NO;
                self.controlView.horizontalLabel.text   = [NSString stringWithFormat:@"%@ %@ / %@",style, currentTime, totalTime];
            }
        }else {

            slider.value = 0;
        }

    }else {

        slider.value = 0;
    }
}

Here what it looks like while scrolling the player: enter image description here

0

There are 0 answers