Anthony Artillondio
Anthony Artillondio

Reputation: 43

iPhone: Play videos automatically in a sequence, one after another

i built a very basic video player app in Xcode 4.3.2 i have 3 small videos and on my main menu i have 3 buttons. One button for each video my code looks like this.....

- (IBAction)playMoviePressed1:(id)sender 
{

NSString *path = [[NSBundle mainBundle]
                  pathForResource:@"HomeVideoOne" ofType:@"m4v"];

player = [[MPMoviePlayerViewController alloc]
          initWithContentURL:[NSURL fileURLWithPath:path]];

[self presentMoviePlayerViewControllerAnimated:player];
}

And it repeats for videos 2 and 3. I would like to have a 4th button that played all 3 videos in order without stopping and having to select the next one. It's almost like having to play chapters in a movie one at a time.

Here's the kicker, I don't want to make any extra video files to add to the project size. So in other words the only way i am able to accomplish my goal so far is to edit all 3 videos into one .m4v file and import that to the project. But that is no good because it doubles my project size. I'd like to call on the existing files to play in one right after another with no break. I hope I didn't sound to repetitious. Thank You -ANthony

Upvotes: 2

Views: 4673

Answers (2)

tipycalFlow
tipycalFlow

Reputation: 7644

You can always start another video whenever a video is finished playing. One way is to set your custom methods to be called to control the behavior when you create youMoviePlayerController:

[[NSNotificationCenter defaultCenter] addObserver:self 
               selector:@selector(aMoviePlaybackComplete:)
                   name:MPMoviePlayerPlaybackDidFinishNotification
                 object:youMoviePlayerController];

and then:

- (void)aMoviePlaybackComplete:(NSNotification*)notification
{
  MPMoviePlayerController *moviePlayerController = [notification object];
  if(urlIndex < yourUrlArray.count){
     NSUrl* myUrl = [yourUrlArray objectAtIndex:urlIndex];
     [moviePlayerController setContentURL:myUrl];
     [moviePlayerController play];
     urlIndex++;
  }
  else{
     [[NSNotificationCenter defaultCenter] removeObserver:self
                                                name:MPMoviePlayerPlaybackDidFinishNotification
                                              object:moviePlayerController];

     [moviePlayerController.view removeFromSuperview];
     [moviePlayerController release];
  }
}

I'm assuming yourUrlArray is a pre-defined array of the urls of the movies you want to play and urlIndex is keeping count of the current movie being played. Of course, you'll have to tune this to your needs.

EDIT- For local files, you can form the NSUrl using bundle:

NSString *moviePath = [[NSBundle mainBundle] pathForResource:@"Movie" ofType:@"m4v"];
NSUrl* url = [NSURL fileURLWithPath:moviePath];
[yourUrlArray addObject:url];

and initialize the url array(yourUrlArray) and then continue as posted before.

Upvotes: 0

overboming
overboming

Reputation: 1542

In this case you will need to use some more advanced API like AVQueuePlayer instead of a basic MPMoviePlayerController Try look through the document here first http://developer.apple.com/library/ios/#documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/02_Playback.html

Upvotes: 2

Related Questions