[英]Detect when AvPlayer switch bit rate
在我的應用程序中,我使用AVPlayer讀取一些帶有HLS協議的流(m3u8文件)。 我需要知道在流式傳輸會話期間,客戶端切換比特率的次數 。
我們假設客戶端的帶寬正在增加。 因此客戶端將切換到更高的比特率段。 AVPlayer可以檢測到此開關嗎?
謝謝。
我最近遇到過類似的問題。 解決方案感覺有點hacky,但它在我看到的情況下起作用。 首先,我為新的Access Log通知設置了一個觀察者:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleAVPlayerAccess:)
name:AVPlayerItemNewAccessLogEntryNotification
object:nil];
哪個叫這個功能。 它可能可以優化,但這是基本的想法:
- (void)handleAVPlayerAccess:(NSNotification *)notif {
AVPlayerItemAccessLog *accessLog = [((AVPlayerItem *)notif.object) accessLog];
AVPlayerItemAccessLogEvent *lastEvent = accessLog.events.lastObject;
float lastEventNumber = lastEvent.indicatedBitrate;
if (lastEventNumber != self.lastBitRate) {
//Here is where you can increment a variable to keep track of the number of times you switch your bit rate.
NSLog(@"Switch indicatedBitrate from: %f to: %f", self.lastBitRate, lastEventNumber);
self.lastBitRate = lastEventNumber;
}
}
每次訪問日志都有新條目時,它會檢查最近一個條目的最后指示比特率(播放器項目的訪問日志中的lastObject)。 它將此指示的比特率與存儲來自最后一次更改的比特率的屬性進行比較。
BoardProgrammer的解決方案效果很好! 在我的情況下,我需要指示的比特率來檢測內容質量何時從SD切換到HD。 這是Swift 3版本。
// Add observer.
NotificationCenter.default.addObserver(self,
selector: #selector(handleAVPlayerAccess),
name: NSNotification.Name.AVPlayerItemNewAccessLogEntry,
object: nil)
// Handle notification.
func handleAVPlayerAccess(notification: Notification) {
guard let playerItem = notification.object as? AVPlayerItem,
let lastEvent = playerItem.accessLog()?.events.last else {
return
}
let indicatedBitrate = lastEvent.indicatedBitrate
// Use bitrate to determine bandwidth decrease or increase.
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.