Video On The iPhone Tutorial Part 4, using AVQueuePlayer to play a series of videos in sequence

AVQueuePlayer allows you to queue up a “playlist” of videos which are then automatically played one after the other. You can also set up a Notification to listen for changes in the AVQueuePlayer’s AVPlayerItems and then build custom code to repeat, skip or add new video items to the queue.
This tut follows on from the previous example Part 3 (Before you start, make sure you add the AVFoundation.framework to your project, otherwise it won’t work, it has been added to the example project), I’ve started with a view based project from the XCode default projects. Here is the .h file of the view:

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@class AVPlayerDemoPlaybackView;

@interface AVFoundation_Test3ViewController : UIViewController {
    AVQueuePlayer *queuePlayer;
    IBOutlet AVPlayerDemoPlaybackView  *mPlaybackView;
}
@property (readwrite, retain) AVQueuePlayer *queuePlayer;
@property (nonatomic, retain) IBOutlet AVPlayerDemoPlaybackView *mPlaybackView;

- (void)observeValueForKeyPath:(NSString*) path ofObject:(id)object change:(NSDictionary*)change context:(void*)context;
@end

and here is the .m file of the view:

#import "AVFoundation_Test3ViewController.h"
static void *AVPlayerDemoPlaybackViewControllerStatusObservationContext = &AVPlayerDemoPlaybackViewControllerStatusObservationContext;

@implementation AVFoundation_Test3ViewController
@synthesize mPlaybackView, queuePlayer;

- (void)dealloc
{
    [queuePlayer release];
    [mPlaybackView release];
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    NSString *secondVideoPath = [[NSBundle mainBundle] pathForResource:@"littleVid3" ofType:@"mp4"];
	NSString *firstVideoPath = [[NSBundle mainBundle] pathForResource:@"littleVid4" ofType:@"mov"];
	
	
	AVPlayerItem *firstVideoItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:firstVideoPath]];
	AVPlayerItem *secondVideoItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:secondVideoPath]];
    
	self.queuePlayer = [AVQueuePlayer queuePlayerWithItems:[NSArray arrayWithObjects:firstVideoItem, secondVideoItem,nil]];
	[self.mPlaybackView setPlayer:self.queuePlayer];
	[self.queuePlayer play];
    
}

@end

So we’re creating two AVPlayerItems out from videos in the application bundle. these two video items are going to become our playlist (of two videos), we add these to an array and then create the queue with this array. Then we set the player of this queue to be the UIPlayerView in our xib file.
if you want to add more videos all you have to do is create more AVPlayerItem’s and add them to the array. You can get the project files here

11 comments

  1. hi, SDK Boy.
    I’m newbie for develop iphone app.
    but, is have any way to use memory base decode function under AV Foundation?
    i just do decode some clip buffer not at all file.
    but, i dont have any idea. thx.

    Rank Liu

    Reply

  2. I use your sample cord.Thank you! But in this cord I have a bit time lag between two movies (in my simulator). How can I solve this problem? Please help me.

    Reply

    1. Hey there Yuto, the lag you see will most probably be because you are playing the example in the simulator. I suggest publishing to an iPhone. I tested it on iPhone 4 and it worked just fine. To do this you’ll have to sign up as an Apple developer

      Reply

  3. Thanks for this! however I tried to tun in on my iPhone 4 (iOS 5), the files are play simultanously but one after another.

    Reply

  4. Hey,
    I am trying to have 6 AVPlayers on the same screen and I do have some problems. First 4 players that loads are playing, the other 2 – just ignored. Do you have some ideas?

    Reply

  5. Cool series of projects. Can you tell me how to loop a videosequence over and over, without pause or dropout ? is that posible ? your queue example work good but that not entierly not a real loop of the same sequence

    Reply

  6. You portrait the subject matter in a really different way.
    Can you reveal a secret or two on how you normally go
    about when writing a post like this?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *