由于差距,AVPlayer流在后台停止 [英] AVPlayer streaming stops in background due to gaps

查看:94
本文介绍了由于差距,AVPlayer流在后台停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于应用在后台模式下,iOS闲置了我的AVPlayer,因此我试图实现无缝播放.

I'm attempting to achieve gapless playback due to iOS idling my AVPlayer when the app is in background mode.

在回答之前,是的,我已经修改了plist,正确设置了音频会话,一切都很好,但我相信我的问题本质上是不同的.

Before answering, yes, I have the plist modified, audio session properly setup and everything is fine and dandy, however my question, I believe, is different in nature.

因此,起初,我在班级中有一个方法,每次更改轨道URL时都会初始化一个新的AVPlayer,它工作得很好,但是在后台运行的却不是很多,尽管我每次都会创建一个全新的AVPlayer实例,但是不能正常工作,所以我切换到其他几个范例,例如replaceCurrentItemWithPlayerItem保持AVPlayer实例保持活动状态,甚至是AVQueuePlayer.

So, at first I had a method in my class to initialize a new AVPlayer everytime the track URL changed, it worked well but on background not so much, I though that creating a brand new AVPlayer instance every time won't work so I switched to few other paradigms, like replaceCurrentItemWithPlayerItem keeping the AVPlayer instance alive, or even AVQueuePlayer.

我面临的问题是,在iOS 6上,音频停止的那一刻,因为其中一首曲目已结束而另一首曲目仍需要加载,iPhone会在后台冻结我的应用程序. 如果我再次使我的应用程序处于前台,则播放将正常恢复.

The problem I'm facing is that, on iOS 6, the moment audio stops, because one track finished and the other one still has to be loaded, the iPhone freezes my app in background. If I get my app in foreground again, playback resumes normally.

由于我没有连续流,而是需要单击单个URL才能开始流式传输,所以我虽然考虑使用AVPlayerQueueinsertItem:next/advanceToNextItem策略,但尝试不让任何最小的音频中断来冻结音频.应用程序.

Since I do not have a continuos stream, but rather individual URLs to hit to begin streaming, I though about using AVPlayerQueue and insertItem:next/advanceToNextItem strategies, attempting to not let any minimal audio interruption to freeze the app in background.

但是问题仍然存在,因为我必须按需在队列中添加新曲目,并且在它缓冲时,iOS已经认为我的应用值得停止.因此,我需要在应用程序的生命周期中有适当的时间来insertItem.

The problem however remains, since I have to add the new track in the queue on demand and by the time it buffers iOS already thinks my app is worth stopping. So I need the proper time in the lifecycle of the app to insertItem.

然后我试图收听此KVO通知:

I was then trying to listen to this KVO notification:

[self.player.currentItem addObserver:self forKeyPath:@"playbackBufferFull" options:0 context:MyPlayerItemContext];

[self.player.currentItem addObserver:self forKeyPath:@"playbackBufferFull" options:0 context:MyPlayerItemContext];

但是永远不会被调用.这样做的目的是跟踪currentItem上的缓冲区已满,然后将下一个缓冲区追加到播放列表中,以便AVQueuePlayer 可能尝试对其进行预缓冲.

but it never, ever gets called. The idea was to keep track of the buffer full on the currentItem and append the next one in playlist so AVQueuePlayer might try to prebuffer it.

由于从未调用过playbackBufferFull,所以另一个想法是尝试获取当前时间/持续时间,计算出90%的播放位置,并在适当的时间触发事件以开始缓冲下一首曲目.

Since playbackBufferFull never gets called another idea is to attempt and grab the current time/duration, calculate where a 90% of the playback might be and fire an event at the appropriate time to begin buffering the next track.

考虑到对象的持续时间属性会并且可能会基于渐进式传入数据而发生变化,这听起来是否很糟糕?

Does it sound bad, given that duration properties on objects will and might change based on the progressive incoming data?

我还能采取什么其他策略?

What other strategies can I take?

谢谢.

推荐答案

我通过结合使用AVQueuePlayer和AVPlayerActionAtItemEndAdvance来解决了这个问题.

I solved it by using a combination of AVQueuePlayer and AVPlayerActionAtItemEndAdvance.

这很棘手,但是有效.

这篇关于由于差距,AVPlayer流在后台停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆