循环 AVPlayer 视频流并刷新每个循环的比特率

Looping an AVPlayer video stream and refreshing the bitrate on each loop

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

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 将为您提供该比特率的最佳流,但如果网络条件发生变化,仍然可以 downgrade/upgrade 流式传输。

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