简体   繁体   English

当 AVPlayer 进入后台时切换到纯音频 HLS 再现

[英]Switch to audio-only HLS rendition when AVPlayer goes into the background

Let's say I have an HLS video in an AVPlayer and I want the audio to continue while the app gets backgrounded.假设我在 AVPlayer 中有一个 HLS 视频,我希望在应用程序进入后台时继续播放音频。

yay I was able to do that with this:是的,我能够做到这一点:

  1. add the Background Modes: Audio capabilities to the application to give my app permission to do this.将背景模式:音频功能添加到应用程序以授予我的应用程序执行此操作的权限。 Described here 在这里描述
  2. in AppDelegate wait for applicationDidEnterBackground and set player = nil on the AVPlayerViewController & save a reference to the player在 AppDelegate 中等待 applicationDidEnterBackground 并在 AVPlayerViewController 上设置 player = nil 并保存对播放器的引用
  3. in AppDelegate wait for applicationWillEnterForeground and set player = savedPlayerReference Described here在 AppDelegate 等待 applicationWillEnterForeground 并设置player = savedPlayerReference 在这里描述

Now, get this.现在,得到这个。 Let's say my HLS master manifest has a few video + audio renditions and then one rendition that is an audio-only track.假设我的 HLS 主清单有一些视频 + 音频再现,然后是一个只有音频轨道的再现。 IDEALLY, when AVPlayer goes to the background and stops playing video, I want AVPlayer to use the audio-only track from the master manifest (in order to save bandwidth and not make the application demux the audio/video tracks and decode the video that is not being shown).理想情况下,当 AVPlayer 进入后台并停止播放视频时,我希望 AVPlayer 使用主清单中的纯音频轨道(为了节省带宽,而不是让应用程序对音频/视频轨道进行解复用并解码视频未显示)。

How can I do this?我怎样才能做到这一点? I thought it might automatically behave that way but from inspecting the network traffic it looks like when the app is backgrounded AVPlayer is staying on the last rendition and it never switches over to the audio-only rendition manifest我认为它可能会自动以这种方式运行,但是从检查网络流量来看,当应用程序处于后台时,AVPlayer 会停留在最后一个再现上,并且它永远不会切换到纯音频再现清单

I figured out the answer, it feels like a bit of a hack, but it's working.我想出了答案,感觉有点像黑客,但它正在工作。

The solution is that before disconnecting, to get access to the underlying AVPlayerItem instance and set preferredPeakBitRate to a level that is expected for the audio rendition ( 300000 for example)解决方案是在断开连接之前访问底层 AVPlayerItem 实例并将preferredPeakBitRate设置为音频再现所期望的级别(例如300000

Something like this:像这样的东西:

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;
}

And then when the application enters back in the foreground re-setting the preferredPeakBitRate to 0 so that the video track comes back然后当应用程序返回前台时,将preferredPeakBitRate重新设置为0,以便视频轨道返回

func applicationWillEnterForeground(_ application: UIApplication) {
  // unset our preferredPeakBitRate value
  playerViewController.player?.currentItem?.preferredPeakBitRate = 0
  // re-connect AVPlayer to the presentation
  playerViewController.player = savedPlayer;
}

I should note that in other contexts (like creating a quality selector or trying to have more control over the specific video rendition), I have not seen preferredPeakBitRate work reliably.我应该注意到,在其他情况下(例如创建质量选择器或尝试对特定视频再现进行更多控制),我还没有看到preferredPeakBitRate可靠地工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM