我有一个正在流式HLS流的Avplayer。
当用户多任务应用程序时,我会看到播放率下降到0.0(暂停),当用户返回时,它返回到1.0(play),但是从暂停的点开始播放。
什么是迫使玩家重新生活而不完全重新启动流的最佳方法?是否有一个seektotime方法可以处理最接近的实时时间参数?
谢谢!
i使用:
double time = MAXFLOAT;
[player seekToTime: CMTimeMakeWithSeconds(time, NSEC_PER_SEC)];
在我的应用中效果很好。
假设播放器是一个avplayer实例:
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)];
igor kulagin的快速版本答案:
player.seek(to: kCMTimePositiveInfinity)
player.play()
在任何情况下都可以完美地工作。其他解决方案给了我NaN
错误计算livePosition
值,或直接与CMTime
一起工作的{INVALID}
错误。
swift 3.0版本
public func resumeLive() {
guard let livePosition = player.currentItem?.seekableTimeRanges.last as? CMTimeRange else {
return
}
player.seek(to:CMTimeRangeGetEnd(livePosition))
}
卡里姆·穆拉(Karim Mourra)答案的迅速版本:
let seekableRanges = player.currentItem!.seekableTimeRanges
guard seekableRanges.count > 0 else {
return
}
let range = seekableRanges.last!.CMTimeRangeValue
let livePosition = range.start + range.duration
let minus = CMTimeMakeWithSeconds(Float64(timeOffset), Int32(NSEC_PER_SEC))
let time = livePosition - minus
player.seekToTime(time)
swift 4版本:
if let seekableRange = player.currentItem?.seekableTimeRanges.last?.timeRangeValue {
let seekableStart = seekableRange.start
let seekableDuration = seekableRange.duration
let livePosition = seekableStart + seekableDuration
player.seek(to: livePosition, completionHandler: { [weak self] _ in
self?.player.play()
})
}
如果使用Apple的CMTimeRange
操纵功能,则无需转换为浮点:
NSValue *value = player.currentItem.seekableTimeRanges.lastObject;
if (value) {
CMTimeRange seekableRange = [value CMTimeRangeValue];
CMTime latestTime = CMTimeRangeGetEnd(seekableRange);
[player seekToTime:latestTime];
} else {
// there are no seekable time ranges
}
请参阅下面的 @Fabian评论。