AVPlayer“冻结"开始缓冲音频流时的应用程序 [英] AVPlayer "freezes" the app at the start of buffering an audio stream
问题描述
我正在使用 AVQueuePlayer
的子类,当我添加带有流 URL 的新 AVPlayerItem
时,应用程序会冻结大约一两秒钟.冻结我的意思是它不响应用户界面上的触摸.此外,如果我已经播放了一首歌曲,然后将另一首歌曲添加到队列中,AVQueuePlayer
会自动开始预加载歌曲,同时它仍在播放第一首歌曲.这使得应用程序在两秒钟内不会响应用户界面上的触摸,就像添加第一首歌曲但歌曲仍在播放时一样.所以这意味着 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 通过使用此方法调用来获取流媒体 URL:
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
它是同步的.当您收到带有 url 的响应时,请尝试以下操作:
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屋!