[英]Is there a way to distinguish between a live stream and an on-demand file stream with AVPlayer?
I'm trying to create a more generic media controller for several types of streaming media and want to adapt the UI to the type of stream;我正在尝试为几种类型的流媒体创建更通用的媒体 controller,并希望使 UI 适应 stream 的类型;
Is there any way to determine from the AVPlayer (or perhaps the AVPlayerItem or AVAsset) what the type of stream is?有什么方法可以从 AVPlayer(或者可能是 AVPlayerItem 或 AVAsset)确定 stream 的类型是什么?
The duration of live video is indefinite :实时视频的持续时间是不确定的:
AVPlayer * player = ...;
const BOOL isLive = CMTIME_IS_INDEFINITE([player currentItem].duration);
You have to check the duration only when the AVPlayerItem
item status is AVPlayerItemStatusReadyToPlay
.仅当
AVPlayerItem
项目状态为AVPlayerItemStatusReadyToPlay
时,您才需要检查持续时间。
For those who are still looking for this feature,对于那些仍在寻找此功能的人,
AVPlayerItem > AVPlayerItemAccessLogEvent > playbackType property might be helpful. AVPlayerItem > AVPlayerItemAccessLogEvent > playbackType 属性可能会有所帮助。 I already checked "VOD", "LIVE" types were appropriately returned from it.
我已经检查了“VOD”,“LIVE”类型已正确返回。
It appears that this is not possible.看来这是不可能的。
However, one could check the duration of a live stream, which seems to be consistently above 33000 seconds.但是,可以检查实时 stream 的持续时间,该持续时间似乎始终高于 33000 秒。 However, this value still fluctuates and checking for this is undesirable, since it might cause unexpected behavior.
但是,此值仍然会波动,并且不希望对此进行检查,因为它可能会导致意外行为。
You can use this code to easily detect the playback type:您可以使用此代码轻松检测播放类型:
NotificationCenter.default.addObserver(
forName: NSNotification.Name.AVPlayerItemNewAccessLogEntry,
object: nil,
queue: OperationQueue.main) { [weak self] (notification) in
guard let self = self else { return }
guard let playerItem = notification.object as? AVPlayerItem,
let lastEvent = playerItem.accessLog()?.events.last else {
return
}
// Here you can set the type (LIVE | VOD | FILE or unknow if it's a nil):
print("Playback Type: \(lastEvent.playbackType ?? "NA")")
}
Add the observer code to where you normally start to listen to them.将观察者代码添加到您通常开始收听它们的位置。
Also, don't forget to remove the observer at the deinit;)另外,不要忘记在 deinit 处删除观察者;)
deinit {
NotificationCenter.default.removeObserver(self,
name: NSNotification.Name.AVPlayerItemNewAccessLogEntry,
object: self)
}
Hope this will help someone:)希望这会对某人有所帮助:)
player?.addPeriodicTimeObserver(forInterval: interval, queue: .main, using: { time in
let playbackType = self.player?.currentItem?.accessLog()?.events.last?.playbackType!
print("Playback Type: \(lastEvent.playbackType ?? "NA")")
if playbackType == StreamingType.Live.rawValue {
}
else if playbackType == StreamingType.Vod.rawValue {
}
})
The playback type can be live, VOD, or from a file.播放类型可以是直播、VOD 或来自文件。 If nil is returned the playback type is unknown.
如果返回 nil,则播放类型未知。 more detail in here
更多细节在这里
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.