AVQueuePlayer playing several audio tracks in background iOS5

强颜欢笑 提交于 2019-12-12 09:18:55

问题


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 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."

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


回答1:


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.

So my solution is:

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

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;

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;
    }

}];

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



来源:https://stackoverflow.com/questions/8201471/avqueueplayer-playing-several-audio-tracks-in-background-ios5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!