How to get Duration from AVPlayer (Not AVAudioPlayer)?

前端 未结 7 1091
隐瞒了意图╮
隐瞒了意图╮ 2020-12-13 03:43

I would like to make a UISlider(scrubber) for my AVPlayer. But since this is not an AVAudioPlayer, it doesn\'t have a built in duration. Any suggestion on how to create the

相关标签:
7条回答
  • 2020-12-13 04:37

    In this example avPlayer is the AVPlayer instance.

    I have built a video control that uses the following:

    to position the slider use something like this to get playhead's percentage through the movie, you will need to fire this function repeatedly. So i would run the function like:

    float scrubberBarLocation = (scrubberBgImageView.frame.size.width / 100.0f) * [self moviePercentage];
    
    
    - (float)moviePercentage {
    
        CMTime t1 = [avPlayer currentTime];
        CMTime t2 = avPlayer.currentItem.asset.duration;
    
        float myCurrentTime = CMTimeGetSeconds(t1);
        float myDuration = CMTimeGetSeconds(t2);
    
        float percent = (myCurrentTime / myDuration)*100.0f;
        return percent;
    
    }
    

    Then to update the video I would do something like:

    - (void)updateVideoPercent:(float)thisPercent {
    
        CMTime t2 = avPlayer.currentItem.asset.duration;
        float myDuration = CMTimeGetSeconds(t2);
    
        float result = myDuration * thisPercent /100.0f;
    
        //NSLog(@"this result = %f",result); // debug
    
        CMTime seekTime = CMTimeMake(result, 1);
    
        [avPlayer seekToTime:seekTime];
    
    }
    
    0 讨论(0)
提交回复
热议问题