我的问题如下:正确管理应用状态。 我有几个实现MPMoviePlayerViewcontroller的xib。当应用程序回到前台时,循环视频的灯光(重约100kb)自动播放(我已经处理了应用程序状态以便正常工作)。
在第一个Xib中,视频在返回前景时立即播放。第二个Xib需要更多时间。第3个Xib需要更多的时间来继续自动播放,而在第4个Xib以后,自动播放需要10秒钟。当应用程序从后台返回到前台时,黑屏会花费大量时间,直到它开始自动播放。就像一个Xib影响另一个。
我在每个xib中执行相同的代码,并且随着我的进步,玩家继续自动播放需要更长的时间。请注意,我在播放器上方覆盖按钮以返回或下一个。如何解决之前解释的滞后?
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];
}