在applicationDillResignActive上暂停视频并在applicationDidBecomeActive上播放

时间:2011-11-23 02:23:37

标签: iphone xcode media-player mpmovieplayer

好吧,我的代码全屏播放,没有用户控件(基本上是一个电影)。电影完成后,我的NSNotification将触发并加载视图。

然而,当用户在其中一部电影中点击主页按钮时,它会暂停,但由于我取走了控件,因此无法让它再次播放。我尝试在appDidBecomeActive下的appDelegate.m中放置[playerController play]和[playerController shouldAutoplay],但是它没有在那里定义所以它不知道是什么playerController。

如果用户收到短信或点击主页按钮,有人可以帮助我正确暂停和播放此视频吗?

-(IBAction)playMovie:(id)sender {
NSString *movieUrl = [[NSBundle mainBundle] pathForResource:@"Initiate" ofType:@"m4v"]; playerController = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:movieUrl]];

[playerController.view setFrame: self.view.bounds];    
[self.view addSubview:playerController.view];
playerController.controlStyle = MPMovieControlStyleNone;
[playerController shouldAutoplay];
[playerController play];


[[NSNotificationCenter defaultCenter] addObserver:self
                                           selector:@selector(playbackFinished:) 
                                           name:MPMoviePlayerPlaybackDidFinishNotification
                                           object:playerController];
}
- (void)playbackFinished:(NSNotification*) notification {
 playerController = [notification object];
 [[NSNotificationCenter defaultCenter] 
 removeObserver:self
 name:MPMoviePlayerPlaybackDidFinishNotification
 object:playerController];


ViewController *viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
viewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:viewController animated:YES];

修改

标题

//ViewController.h

@interface ViewController : UIViewController {
MPMoviePlayerController *playerController;
}

实施

//ViewController.m
playerController = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:movieUrl]];

    AppDelegate *sharedAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    sharedAppDelegate.pointer = playerController;

1 个答案:

答案 0 :(得分:2)

实现播放器时,向app delegate发送所有必要的指针:

AppDelegate *sharedAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
sharedAppDelegate.yourPointer = yourPlayer;

使用appDelegate.m中的这个方法:

- (void)applicationWillResignActive:(UIApplication *)application
{
    /*
     Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
     Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
     */
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    /*
     Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
     */
}