【问题标题】:Detect when AvPlayer switch bit rate检测 AvPlayer 何时切换比特率
【发布时间】:2015-05-11 21:51:18
【问题描述】:

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

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

谢谢。

【问题讨论】:

    标签: ios avplayer http-live-streaming


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      BoardProgrammer 的解决方案效果很好!就我而言,我需要指示的比特率来检测内容质量何时从标清切换到高清。这是 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.
      }
      

      【讨论】:

      • @SPatel 你有示例网址可以分享吗?我可以试试看能不能得到日志
      • 我已经设置好了,但是由于某种原因,这个电话似乎从来没有发生过——有什么想法吗?
      猜你喜欢
      • 1970-01-01
      • 2015-12-01
      • 2012-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-04
      • 1970-01-01
      相关资源
      最近更新 更多