检测AvPlayer切换比特率的时间 [英] Detect when AvPlayer switch bit rate

查看:116
本文介绍了检测AvPlayer切换比特率的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my application, I use the AVPlayer to read some streams (m3u8 file), with HLS protocol. I need to know how many times, during a streaming session, the client switches bitrate.

让我们假设客户端的带宽正在增加.因此,客户端将切换到更高的比特率段.AVPlayer可以检测到此开关吗?

Let's assume the client's bandwidth is increasing. So the client will switch to a higher bitrate segment. Can the AVPlayer detect this switch ?

谢谢.

推荐答案

我最近也遇到了类似的问题.该解决方案感觉有点笨拙,但据我所知一直有效.首先,我为新的访问日志通知设置了一个观察器:

I have had a similar problem recently. The solution felt a bit hacky but it worked as far as I saw. First I set up an observer for new Access Log notifications:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(handleAVPlayerAccess:)
                                             name:AVPlayerItemNewAccessLogEntryNotification
                                           object:nil];

哪个调用此函数.可能可以对其进行优化,但这是基本思想:

Which calls this function. It can probably be optimised but here is the basic idea:

- (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)中检查最后指示的比特率.它将指示的比特率与存储了最后一次更改的比特率的属性进行比较.

Every time there is a new entry to the access log, it checks the last indicated bitrate from the most recent entry (the lastObject in the access log for the player item). It compares this indicated bitrate with a property that stored the the bitrate from that last change.

这篇关于检测AvPlayer切换比特率的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆