AVPlayer“冻结”缓冲音频流的应用程序 [英] AVPlayer "freezes" the app at the start of buffering an audio stream

查看:184
本文介绍了AVPlayer“冻结”缓冲音频流的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 AVQueuePlayer 的子类,当我使用流式网址添加新的 AVPlayerItem 时,该应用程序会冻结大概一两秒钟。通过冻结我的意思是它不响应UI上的触摸。此外,如果我已经播放了一首歌然后再将另一首歌添加到队列中,那么 AVQueuePlayer 会自动开始预加载该歌曲,同时它仍在播放第一首歌曲。这使得应用程序不会响应UI上的触摸两秒钟,就像添加第一首歌但歌曲仍在播放时一样。这意味着 AVQueuePlayer 正在主线程中做一些导致明显冻结的事情。

I am using a subclass of AVQueuePlayer and when I add new AVPlayerItem with a streaming URL the app freezes for about a second or two. By freezing I mean that it doesn't respond to touches on the UI. Also, if I have a song playing already and then add another one to the queue, AVQueuePlayer automatically starts preloading the song while it is still streaming the first one. This makes the app not respond to touches on the UI for two seconds just like when adding the first song but the song is still playing. So that means AVQueuePlayer is doing something in main thread that is causing the apparent "freeze".

我正在使用 insertItem:afterItem:添加我的 AVPlayerItem 。我测试并确保这是造成延迟的方法。也许它可能是 AVPlayerItem 在将它添加到队列时被 AVQueuePlayer 激活时所做的事情。

I am using insertItem:afterItem: to add my AVPlayerItem. I tested and made sure that this was the method that was causing the delay. Maybe it could be something that AVPlayerItem does when it gets activated by AVQueuePlayer at the moment of adding it to the queue.

必须指出我使用Dropbox API v1 beta通过此方法调用获取流式网址:

Must point out that I am using the Dropbox API v1 beta to get the streaming URL by using this method call:

[[self restClient] loadStreamableURLForFile:metadata.path];

然后,当我收到流URL时,我将其发送到 AVQueuePlayer 如下:

Then when I receive the stream URL I send it to AVQueuePlayer as follows:

[self.player insertItem:[AVPlayerItem playerItemWithURL:url] afterItem:nil];

所以我的问题是:我该如何避免这种情况?
我应该在没有 AVPlayer 的帮助下自己预加载音频流吗?如果是这样,我该怎么做?

So my question is: How do I avoid this? Should I do the preloading of an audio stream on my own without the help of AVPlayer? If so, how do I do this?

谢谢。

推荐答案

不要使用 playerItemWithURL 它是同步的。
当你收到带网址的回复时,试试这个:

Don't use playerItemWithURL it's sync. When you receive the response with the url try this:

AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:url options:nil];
NSArray *keys = @[@"playable"];

[asset loadValuesAsynchronouslyForKeys:keys completionHandler:^() {
    [self.player insertItem:[AVPlayerItem playerItemWithAsset:asset] afterItem:nil];
}];

这篇关于AVPlayer“冻结”缓冲音频流的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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