我有一个AVPlayer正在流式传输实时HLS流。
当用户多任务应用程序时,我看到播放速率下降到0.0(暂停),当用户返回时它返回到1.0(播放),但是从暂停点开始播放。
在不重新启动流的情况下强制播放器恢复生效的最佳方法是什么?是否有seekToTime方法处理最接近的实时时间参数?
谢谢!
答案 0 :(得分:7)
我用:
double time = MAXFLOAT;
[player seekToTime: CMTimeMakeWithSeconds(time, NSEC_PER_SEC)];
在我的应用中运行良好。
答案 1 :(得分:1)
假设播放器是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)];
答案 2 :(得分:1)
Swift 3.0版本
public func resumeLive() {
guard let livePosition = player.currentItem?.seekableTimeRanges.last as? CMTimeRange else {
return
}
player.seek(to:CMTimeRangeGetEnd(livePosition))
}
答案 3 :(得分:0)
快速版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)
答案 4 :(得分:0)
如果您使用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的评论
答案 5 :(得分:0)
快速版Igor Kulagin回答:
player.seek(to: kCMTimePositiveInfinity)
player.play()
在任何条件下都能完美运作。其他解决方案在计算NaN
值时出现livePosition
错误,或直接与{INVALID}
一起使用时出现CMTime
错误。
答案 6 :(得分:0)
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()
})
}