[英]Switch to audio-only HLS rendition when AVPlayer goes into the background
假设我在 AVPlayer 中有一个 HLS 视频,我希望在应用程序进入后台时继续播放音频。
是的,我能够做到这一点:
player = savedPlayerReference
在这里描述现在,得到这个。 假设我的 HLS 主清单有一些视频 + 音频再现,然后是一个只有音频轨道的再现。 理想情况下,当 AVPlayer 进入后台并停止播放视频时,我希望 AVPlayer 使用主清单中的纯音频轨道(为了节省带宽,而不是让应用程序对音频/视频轨道进行解复用并解码视频未显示)。
我怎样才能做到这一点? 我认为它可能会自动以这种方式运行,但是从检查网络流量来看,当应用程序处于后台时,AVPlayer 会停留在最后一个再现上,并且它永远不会切换到纯音频再现清单
我想出了答案,感觉有点像黑客,但它正在工作。
解决方案是在断开连接之前访问底层 AVPlayerItem 实例并将preferredPeakBitRate设置为音频再现所期望的级别(例如300000
)
像这样的东西:
func applicationDidEnterBackground(_ application: UIApplication) {
// set preferred bitrate to the bitrate we expect from the audio rendition
playerViewController.player?.currentItem?.preferredPeakBitRate = 300000
savedPlayer = playerViewController.player
// disconnect AVPlayer from the presentation
playerViewController.player = nil;
}
然后当应用程序返回前台时,将preferredPeakBitRate
重新设置为0,以便视频轨道返回
func applicationWillEnterForeground(_ application: UIApplication) {
// unset our preferredPeakBitRate value
playerViewController.player?.currentItem?.preferredPeakBitRate = 0
// re-connect AVPlayer to the presentation
playerViewController.player = savedPlayer;
}
我应该注意到,在其他情况下(例如创建质量选择器或尝试对特定视频再现进行更多控制),我还没有看到preferredPeakBitRate
可靠地工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.