AVQueuePlayer在后台iOS5中播放多个音轨 [英] AVQueuePlayer playing several audio tracks in background iOS5

查看:129
本文介绍了AVQueuePlayer在后台iOS5中播放多个音轨的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用AVQueuePlayer在后台播放多个项目。代码在iOS4中运行完美。在iOS5中,AVQueuePlayer改变了它的行为,因此玩家在第一个项目结束后停止播放。

I used AVQueuePlayer to play several items in background. And code worked perfect in iOS4. And in iOS5 AVQueuePlayer changed its behavior, so player stops playing after first item is ended.

Matt Gallagher在此帖子。 从iOS 5开始,似乎AVQueuePlayer不再预先缓冲。它确实预先缓冲了iOS 4中的下一首曲目。

Matt Gallagher wrote a hint in this post. "As of iOS 5, it appears that AVQueuePlayer no longer pre-buffers. It did pre-buffer the next track in iOS 4."

所以我的问题是如何在iOS5中使用AVPlayer或AVQueuePlayer在后台播放几个项目。

So my question is how to play several items in background using AVPlayer or AVQueuePlayer in iOS5.

推荐答案

Matt Gallagher的答案在他的博客
你必须观察AVQueuePlayer中的当前项目。当它更改时,必须使用UIApplication启动backgroundTask,并在收到下一个文件的准备播放通知时才结束后台任务。

Matt Gallagher's answer in his blog: "You must observe the current item in the AVQueuePlayer. When it changes, you must use UIApplication to start a backgroundTask and only end the background task when you receive a ready to play notification for the next file."

实际上,这不是帮帮我。

Actually, this did not helped me.

所以我的解决方案是:

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

我无法解释原因,但在添加新的AVPlayerItem之前放入这行代码使我的代码工作。
也适用于那些在后台添加下一首曲目并使用方法的人

I can't explain why, but this line of code put before adding new AVPlayerItem made my code work. Also for those one who adds next track in background and uses method

- (void)loadValuesAsynchronouslyForKeys:(NSArray *)keys completionHandler:(void (^)(void))handler;

您必须在主线程上向玩家添加AVPlayerItem。
是这样的:

You must add AVPlayerItem to player on the main thread. like this:

- (void)addAsset:(AVAsset*)as
{
    [player insertItem:[AVPlayerItem playerItemWithAsset:as] afterItem:[player currentItem]];
}

.........

//adding new track 

AVURLAsset* as = [[self createAsset:urlString] retain];
NSArray *keys = [NSArray arrayWithObject:@"tracks"];
[as loadValuesAsynchronouslyForKeys:keys completionHandler:^(void) {
NSError *error = 
AVKeyValueStatus trackStatus = [as statusOfValueForKey:@"tracks" error:&error];
switch (trackStatus) {
    case AVKeyValueStatusLoaded:
            [self performSelectorOnMainThread:@selector(addAsset:) withObject:as waitUntilDone:YES];
            [as release];
    break;
    case AVKeyValueStatusFailed:
            [as release];
    break;
    case AVKeyValueStatusCancelled:
            [as release];
    break;
    default:
    break;
    }

}];

更新:
Matt Gallagher是对的,但只有在你不使用异步时才有效loading。

UPDATE: Matt Gallagher was right, but it works only if you do not use asynchronous loading.

这篇关于AVQueuePlayer在后台iOS5中播放多个音轨的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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