检测AvPlayer何时切换比特率

tca*_*ore 7 http-live-streaming ios avplayer

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

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

谢谢.

小智 13

我最近遇到过类似的问题.解决方案感觉有点hacky,但它在我看到的情况下起作用.首先,我为新的Access Log通知设置了一个观察者:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(handleAVPlayerAccess:)
                                             name:AVPlayerItemNewAccessLogEntryNotification
                                           object:nil];
Run Code Online (Sandbox Code Playgroud)

哪个叫这个功能.它可能可以优化,但这是基本的想法:

- (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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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


Nah*_*dan 7

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.
}
Run Code Online (Sandbox Code Playgroud)