【问题标题】:Looping an AVPlayer video stream and refreshing the bitrate on each loop循环播放 AVPlayer 视频流并刷新每个循环的比特率
【发布时间】:2016-07-04 23:42:12
【问题描述】:

我正在通过 .m3u8 播放列表循环播放我的流媒体视频(不是直播),每次视频重新启动时,它都会以与您第一次观看视频时相同的比特率调整播放视频(质量差 -> 好质量)。有没有办法在每次视频循环时刷新流质量,以便无缝地用更高速率的比特率替换开头?而不是仅仅重新播放最初加载的内容?

【问题讨论】:

    标签: ios objective-c swift video avplayer


    【解决方案1】:

    Apple 的 AVPlayer 尝试加载 HLS 播放列表中列出的第一个流。因此,如果您希望默认首先加载最高质量的流,则需要将其指定为播放列表文件中的第一个流。

    考虑到这一点,实现您需要实现的目标的一种方法是为每个流使用不同的 m3u8 文件。 例如,如果您有一个三个变体流播放列表,那么您将有三个 .m3u8 播放列表。

    然后在您使用AVPlayer 的视图控制器中,您需要保留对上次观察到的比特率和最近的比特率的引用:

    var lastObservedBitrate: double = 0
    var mostRecentBitrate: double = 0
    

    然后你需要在你的播放器上注册一个通知观察者,通知名称为:AVPlayerItemNewAccessLogEntryNotification

    NSNotificationCenter.defaultCenter().addObserver(self, selector:#selector(MyViewController.accessEventLog(_:)), name: AVPlayerItemNewAccessLogEntryNotification, object: nil)
    

    每当更新访问日志时,您都可以使用以下代码检查使用的比特率和流:

    func accessLogEvent(notification: NSNotification) {
        guard let item = notification.object as? AVPlayerItem,
            accessLog = item.accessLog() else {
                return
        }
        accessLog.events.forEach { lastEvent in
            let bitrate = lastEvent.indicatedBitrate
            lastObservedBitrate = lastEvent.observedBitrate
            if let mostRecentBitrate = self.mostRecentBitrate where bitrate != mostRecentBitrate {
                self.mostRecentBitrate = bitrate
            }
        }    
    }
    

    每当您的播放器循环播放时,您都可以根据您的lastObservedBitrate 加载相应的m3u8 文件。因此,如果您的 lastObservedBitrate 为 2500 kbps,您将加载文件顶部具有 2500kbps 流的 m3u8 文件。

    无耻插件:我们在视频 api 中设计了类似的东西。您需要做的就是使用您的连接类型请求 m3u8 文件:wificellularlastObservedBitrate,我们的 API 将为您提供该比特率的最佳流,但仍然可以降级/升级如果网络条件发生变化,则进行流式传输。

    如果您有兴趣查看,请访问:https://api.storie.comhttps://github.com/Storie/StorieCloudSDK

    【讨论】:

      猜你喜欢
      • 2016-07-09
      • 1970-01-01
      • 1970-01-01
      • 2011-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-30
      相关资源
      最近更新 更多