Search code examples
iphoneobjective-ciosmpmovieplayercontroller

How to play another video in the same MPMoviePlayerController?


*I'm creating an iPhone app where we can watch videos... I,ve been making my own controls so I want to implement the next, back buttons, I can show theme so for the UI everything is ok the problem is to restart the MoviePlayer with another content... Any idea???

I could use the same configuration when the playback did finish, I mean... If the video ends playing to start playing another one... I've tried to set contentUrl but it trows an exception:

2012-04-17 11:37:41.198 NexTest2[8218:11f03] *** Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer position contains NaN: [nan 11.5]'

viewController.m

- (void)viewDidLoad{
[super viewDidLoad];    
fileURL = [NSURL URLWithString:urlString];
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
moviePlayerController.controlStyle = MPMovieControlStyleNone;
[moviePlayerController setShouldAutoplay:YES];
[self addObservers];   

/* Inset the movie frame in the parent view frame. */
CGRect viewInsetRect = CGRectInset ([self.view bounds],0.0, 0.0 );
[[moviePlayerController view] setFrame: viewInsetRect ];
[self.view addSubview:moviePlayerController.view]; 
[self resizeControlViews];
}

then in resizeControlViews I have this, cause I'm using these two views like controls:

-(void)resizeControlViews{
CGRect barFrame = self.controlBarView.frame;
barFrame.origin.x = round((moviePlayerController.view.frame.size.width - barFrame.size.width) / 2.0);
barFrame.origin.y = round((moviePlayerController.view.frame.size.height - barFrame.size.height)/10.0);
self.controlBarView.frame = barFrame;
[moviePlayerController.view addSubview:controlBarView];
CGRect playFrame = self.controlPlaybackView.frame;
playFrame.origin.x = round((moviePlayerController.view.frame.size.width - playFrame.size.width) / 2.0);
playFrame.origin.y = round((moviePlayerController.view.frame.size.height - playFrame.size.height)/1.1);
self.controlPlaybackView.frame = playFrame; 
[moviePlayerController.view addSubview:controlPlaybackView];
}

Here everything is working fine, in one of these views there is a slider who controls the seeking, and in the other one are the play, next and back buttons. I think maybe the problem is whit the UI components of these views...

I've added these observers to control the playback:

//Add the observers to the player
-(void)addObservers{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackStarted:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil]; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setDurationLabel:) name:MPMovieDurationAvailableNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyWindowChanged:) name:UIWindowDidBecomeKeyNotification object:nil];

}

so to handle when the player is playing I use the MPMoviePlayerPlaybackStateDidChangeNotification with this selector:

//called when the playback state changes of state
- (void)playbackStarted:(NSNotification*)notification {
MPMoviePlayerController *player = notification.object;
if (player.playbackState == MPMoviePlaybackStatePlaying) {
    [self timerRunning];
}
}

and if the player is "playing" I monitor it with this method:

-(void)timerRunning{
self.currentTime.text = [self floatToStringTime:(moviePlayerController.currentPlaybackTime)];
self.timeBar.value = moviePlayerController.currentPlaybackTime / moviePlayerController.duration;

if (moviePlayerController.playbackState == MPMoviePlaybackStatePlaying) {
    [self performSelector:@selector(timerRunning) withObject:nil afterDelay:0.5];
}        
}

so Maybe here is the issue... but with a single video it works perfectly...

I've four where the mistake is... is while i'm monitoring in the line to set the value to the slider, but I really don't know why, I think it is cause when another video or stoping video it is still executed so the player has not duration or current playback what trows this exception... how could I to solve it... here is the mistake... maybe moving this code to another part...

self.timeBar.value = moviePlayerController.currentPlaybackTime / moviePlayerController.duration;

Solution

  • Check your UI related code. You most likely are not properly checking the player status before getting and using content related properties from it.