解决AVPlayerItem 的duration值为nan

// 当AVPlayerStatusReadyToPlay
- (NSTimeInterval)getTotalDuration {
    CMTime totalTime = self.player.currentItem.asset.duration;
    if (@available(iOS 13.0, *)) {
        totalTime = self.player.currentItem.duration;
    }
    NSTimeInterval sec = CMTimeGetSeconds(totalTime);
    NSLog(@"%f", sec);
    if (isnan(sec)) {
        return 0;
    }
    return sec;
}

附录:

- (NSTimeInterval)getCurrentTime {
    CMTime currentTime = self.player.currentItem.currentTime;
    NSTimeInterval sec = CMTimeGetSeconds(currentTime);
    NSLog(@"%f", sec);
    if (isnan(sec)) {
        return 0;
    }
    return sec;
}
//监听播放进度
id timeObserver = [self.player addPeriodicTimeObserverForInterval:CMTimeMake(1,10) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
        @strongify(self);
        NSTimeInterval sec = CMTimeGetSeconds(time);
        self.currentTimeLabel.text = [self getCurrentTimeStringFormCurrentPlaybackTime:sec];
        self.mediaProgressSlider.value = sec;
    }];
//移除监听
[self.player removeTimeObserver: timeObserver];

你可能感兴趣的:(解决AVPlayerItem 的duration值为nan)