My problem is the following: managing properly the app states. I have several xibs implementing MPMoviePlayerViewcontroller. The light (that weight around 100kb) looping videos autoplay when the app comes back to the foreground (I've already handled the app states so that works fine).
In the 1st Xib the video plays immediately when coming back to the foreground. The 2nd Xib takes more time. The 3rd Xib takes even more time to continue autoplay and in the 4th Xib onwards it takes like 10 seconds to autoplay. When the app comes back from the the background to the foreground it takes a lot of time with a black screen until it starts auto play.As if one Xib affects the other.
I do the same code in every xib and as I advance it takes longer for the player continue auto play. Note that I overlay buttons above the player to go back or next. How to solve the lag explained previously?
AppDelegate.h
- (void)applicationWillResignActive:(UIApplication *)application
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"WillResignActive" object:nil];
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"WillResignActive" object:nil];
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"WillEnterForeGround" object:nil];
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"DidBecomeActive" object:nil];
}
ViewController1.h
@synthesize playerController;
-(IBAction)next
{
two *back = [[two alloc]initWithNibName:@"two" bundle:Nil];
back.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:back animated:YES completion:nil ];
[back release];
}
- (void)viewDidLoad{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"MyFile" ofType:@"mov"]];
playerController = [[MPMoviePlayerViewController alloc]initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:playerController];
[self.view insertSubview:playerController.view atIndex:0];
playerController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
playerController.moviePlayer.controlStyle = MPMovieControlStyleNone;
playerController.moviePlayer.scalingMode = MPMovieScalingModeFill;
playerController.moviePlayer.repeatMode = MPMovieRepeatModeOne;
playerController.moviePlayer.view.userInteractionEnabled = NO;
playerController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(AppDidBecomeActive) name:@"DidBecomeActive" object:nil];;
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(EnteredBackground) name:@"WillResignActive" object:nil];;
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(EnteredForeground) name:@"WillEnterForeGround" object:nil];;
[playerController.moviePlayer prepareToPlay];
[playerController.moviePlayer prepareToPlay];
[playerController.moviePlayer play];
}
-(void)AppDidBecomeActive{
if(playerController.moviePlayer.playbackState == MPMoviePlaybackStateInterrupted || playerController.moviePlayer.playbackState == MPMoviePlaybackStateStopped || playerController.moviePlayer.playbackState == MPMoviePlaybackStatePaused)
{
[playerController.moviePlayer play];
}
}
-(void)EnteredBackground
{ [playerController.moviePlayer pause];
}
-(void)EnteredForeground
{ [playerController.moviePlayer play];
}