![](/img/trans.png)
[英]Is there a way to force an AVPlayer audio stream to always play live with limited buffering?
[英]Is there a way to distinguish between a live stream and an on-demand file stream with AVPlayer?
我正在嘗試為幾種類型的流媒體創建更通用的媒體 controller,並希望使 UI 適應 stream 的類型;
有什么方法可以從 AVPlayer(或者可能是 AVPlayerItem 或 AVAsset)確定 stream 的類型是什么?
實時視頻的持續時間是不確定的:
AVPlayer * player = ...;
const BOOL isLive = CMTIME_IS_INDEFINITE([player currentItem].duration);
僅當AVPlayerItem
項目狀態為AVPlayerItemStatusReadyToPlay
時,您才需要檢查持續時間。
對於那些仍在尋找此功能的人,
AVPlayerItem > AVPlayerItemAccessLogEvent > playbackType 屬性可能會有所幫助。 我已經檢查了“VOD”,“LIVE”類型已正確返回。
更多細節在這里
看來這是不可能的。
但是,可以檢查實時 stream 的持續時間,該持續時間似乎始終高於 33000 秒。 但是,此值仍然會波動,並且不希望對此進行檢查,因為它可能會導致意外行為。
您可以使用此代碼輕松檢測播放類型:
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")")
}
將觀察者代碼添加到您通常開始收聽它們的位置。
另外,不要忘記在 deinit 處刪除觀察者;)
deinit {
NotificationCenter.default.removeObserver(self,
name: NSNotification.Name.AVPlayerItemNewAccessLogEntry,
object: self)
}
希望這會對某人有所幫助:)
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 {
}
})
播放類型可以是直播、VOD 或來自文件。 如果返回 nil,則播放類型未知。 更多細節在這里
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.