当AvPlayer切换比特率时检测

在我的应用程序中,我使用AVPlayer用HLS协议读取一些stream(m3u8文件)。 我需要知道在stream会话期间客户端切换比特率的次数

假设客户的带宽正在增加。 所以客户端将切换到更高的比特率段。 AVPlayer能检测到这个开关吗?

谢谢。

我最近有类似的问题。 解决scheme感觉有点不好意思,但据我所知,它工作。 首先,我为新的访问日志通知设置了一个观察者:

[[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; } } 

每次访问日志中都有一个新的条目时,它会检查最近一次条目(播放器条目的访问日志中的最后一个条目)的最后一个指定比特率。 它将此指示的比特率与存储最后一次更改的比特率的属性进行比较。

BoardProgrammer的解决scheme效果很好! 在我的情况下,我需要指示的比特率来检测内容质量何时从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. }