使用 AVPlayer 播放音频无法在后台运行
我正在使用远程 URL 中的 AVPlayer 类进行音频播放。
如果我只用耳机/内置扬声器播放,它播放得很好,但我发现 AirPlay 有一些奇怪的地方。
当我更改为使用 AppleTV 进行 AirPlay 时,它会传输音频,但在我的 iPhone 上,音频播放指示器(顶部菜单栏中的小“播放”三角形)消失了,当我锁定手机而不是继续播放时,背景,它停止了。 (正常播放时,它在后台正常播放)。
为什么 AVPlayer 无法在后台通过 AirPlay 播放音频?
这是别人看到的吗?有办法解决吗?
I am doing audio playback using the AVPlayer class from a remote URL.
It plays fine if I just play to the headphones/built in speaker, but I am seeing some oddities with AirPlay.
When I change to use an AppleTV for AirPlay, it will stream the audio, but on my iphone the audio playback indicator (the small 'play' triangle in the top menu bar) disappears, and when I lock the phone instead of continuing in the background, it stops. (When playing normally, it plays in the background fine).
Why is the AVPlayer not working in the background for audio over AirPlay?
Is this what other see? Is there a way around it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的 - 我浏览了苹果开发论坛并找到了提示。
如果您仅将 AVPlayer 用于音频,并希望它在播放时在后台工作,则需要禁用
allowsAirPlayVideo
设置。显然,iOS 5+ 上的 AVPlayer 假定它正在通过 AirPlay 播放视频,因此不允许后台播放,除非您明确禁用视频 AirPlay。
一旦禁用此功能(即
self.player.allowsAirPlayVideo = FALSE;
),您的音频仍将通过 AirPlay 播放,但现在它不会被视为视频,仅被视为音频,因此将允许工作背景。Ok - I dug through the apple dev forums and found a hint.
If you are using AVPlayer for audio only, and want it to work in the background while doing airplay, you need to disable the
allowsAirPlayVideo
setting.Apparently AVPlayer on iOS 5+ assumes that it is playing back video via AirPlay, and so does not allow backgrounding, unless you explicitly disable video AirPlay.
Once you disable this (i.e.
self.player.allowsAirPlayVideo = FALSE;
) then your audio will still play via AirPlay, but now it will not be treated as video, only as audio, and so will allow the backgrounding to work.您是否在应用程序 plist 中启用了“背景音频”多任务设置?
Have you enable the 'Background Audio' mutlitasking setting in the app plist?