tguclu
tguclu

Reputation: 719

MPMoviePlayerViewController can not play mp4 file

I'm working on a test application that will run an mp4 file from internet. code is :

-(IBAction)playRemoteVideo
{

    NSString *mp4File = @"http://archive.org/download/Pbtestfilemp4videotestmp4/video_test_512kb.mp4";

    MPMoviePlayerViewController *playerController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:mp4File]];
    [self presentMoviePlayerViewControllerAnimated:playerController];
    playerController.moviePlayer.movieSourceType=MPMovieSourceTypeStreaming;

    [playerController.moviePlayer play];
    [playerController release];
    playerController=nil;
}

When I run the application and played the video the player tries to load the video for a while but after I got this exception on console

2012-04-18 22:45:11.309 VideoPlayer[891:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'An AVPlayerItem can occupy only one position in a player's queue at a time.'
*** First throw call stack:
(0x1df1052 0x1333d0a 0x27cfb31 0x27cbb2a 0x27e45cc 0x103b73 0xd4e6a 0x2ff2445 0x2ff44f0 0x1d28833 0x1d27db4 0x1d27ccb 0x16d8879 0x16d893e 0x24ea9b 0x1d12 0x1c85)
terminate called throwing an exception(gdb)

If I execute the same code with an m3u8 file , for instance;

http://devimages.apple.com/iphone/samples/bipbop/gear1/prog_index.m3u8

I can get the video running but same does not work for mp4 file.

Do you have any idea why I got this exception and what's wrong with my code?

I run the application on Iphone simulator and I have XCode 4.2 Best Regards Tugrul

Upvotes: 2

Views: 5330

Answers (3)

vatti
vatti

Reputation: 473

I know this error. Try this code here.

 MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL: url];
    self.movieController = player;
    self.movieController.fullscreen = YES;
    self.movieController.controlStyle = MPMovieControlStyleDefault;

    // Register to receive a notification when the movie has finished playing.  
    [[NSNotificationCenter defaultCenter] addObserver:self  
                                             selector:@selector(moviePlayBackDidFinish:)  
                                                 name:MPMoviePlayerPlaybackDidFinishNotification  
                                               object:self.movieController];  
    // Register to receive a notification when the movie has finished playing.  
    [[NSNotificationCenter defaultCenter] addObserver:self  
                                             selector:@selector(moviePlayBackDidFinish:)  
                                                 name:MPMoviePlayerDidExitFullscreenNotification
                                               object:self.movieController];  
    [self.movieController prepareToPlay];
    [self.movieController.view setFrame: self.view.bounds];  // player's frame must match parent's
    [self.view addSubview: self.movieController.view];

    // ...
    [self.movieController play];

Upvotes: 0

Dev Kanchen
Dev Kanchen

Reputation: 2362

One more scenario where this can happen is, as the log quite helpfully tells you, when "An AVPlayerItem can occupy only one position in a player's queue at a time." Basically, when you're trying to get two videos to start playing at the same time, or even INTERACT with two videos/MPMoviePlayerController objects at the same time.

In my app I use two MPMoviePlayerController's and keep swapping them around to create the illusion for the user of infinitely moving and swapping between different videos.

This was working fine so far, but I recently added notifications for some events that should result in the videos being paused and resumed. However I didn't realize both my player objects were listening to the notifications at the same time, and hence tried to trigger either "Pause" or "Play" at the same time. This caused the framework to think I was trying to play multiple videos at the same time, and threw this exception.

All I had to do now was make sure that only one player object was listening to notifications at any given point. Just a small tweak in my application logic.

So if you're getting a weird error, it need not be the framework that has a problem, a malicious operating system bent on making your life hell or an act of god. It could just be pure, good old-fashioned bad code. :)

Upvotes: 0

CodaFi
CodaFi

Reputation: 43330

Set the movie player's control style like so.

[self.mPlayer.moviePlayer setControlStyle:MPMovieControlStyleDefault];

Also, local files have the MPMovieSourceTypeFile, not MPMovieSourceTypeStreaming property set.

Upvotes: 4

Related Questions