AVPlayer Not Loading Media In Background

不打扰是莪最后的温柔 提交于 2019-12-06 11:35:35
Rhythmic Fistman

Try calling beginBackgroundTask before you start playing. The documentation says it shouldn't be used to run in the background, but it also says it's useful for unfinished business, which I believe describes your situation.

My reasoning is this:

  1. for your app to run in the background under the audio background mode you must be playing audio when iOS would normally deschedule you
  2. when playing remote media, it naturally requires more time than local media from between when you tell the AVPlayer to play and when audio actually starts playing, allowing your app to continue.

Hence the background task to give your app a bit more time (some say as much as 3 minutes) to load the remote media and play the audio.

Should you have to do this trick? Probably not - this is an AVPlayer/iOS scheduler implementation detail - for the iOS scheduler to be "fair" in the background audio case, it really ought to recognise your "best effort" attempt to play audio by letting AVPlayer.play() mark the beginning of background audio. If the playback fails for whatever reason, then fine, deschedule. Anyhow, if you feel strongly about it you can file a feature request.

p.s. don't forget to call endBackgroundTask! Both in the expiration handler and, to be a good mobile device citizen, as soon as audio has started playing, assuring your continuing background running. If endBackgroundTask is affecting your ability to play audio in the background, then you're calling it too early!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!