How to get the exact time position of a live broadcast in avplayer

I am using AVPlayer to play streaming video. This stream supports one-hour catch-up, which means that the user can search an hour ago and play. But I have one question, how to find out the exact position that the player is playing. I need to display the current position on the player screen. For example, if the user plays half an hour ago, then -30: 00 is displayed; if the user plays the last content, the player will show 00:00 or live. Thanks

+4
source share
2 answers

To get the living position of the poison and look for it, you can use seekableTimeRangesof AVPlayerItem:

CMTimeRange seekableRange = [player.currentItem.seekableTimeRanges.lastObject CMTimeRangeValue];
CGFloat seekableStart = CMTimeGetSeconds(seekableRange.start);
CGFloat seekableDuration = CMTimeGetSeconds(seekableRange.duration);
CGFloat livePosition = seekableStart + seekableDuration;

[player seekToTime:CMTimeMake(livePosition, 1)];

Also, when you search some time ago, you can get the current playback position by calling the method currentTime

CGFloat current = CMTimeGetSeconds([self.player.currentItem currentTime]);
GCFloat diff = livePosition - current;
+2
source

Swift Solution:

   override func getLiveDuration() -> Float {
    var result : Float = 0.0;

    if let items = player.currentItem?.seekableTimeRanges {

        if(!items.isEmpty) {
            let range = items[items.count - 1]
            let timeRange = range.timeRangeValue
            let startSeconds = CMTimeGetSeconds(timeRange.start)
            let durationSeconds = CMTimeGetSeconds(timeRange.duration)

            result = Float(startSeconds + durationSeconds)
        }

    }
    return result;
}
+2
source

All Articles