Приостановить воспроизведение видео после приложения «WillResignActive» и «Воспроизвести после приложения» DIDBecomeActive - PullRequest
1 голос
/ 23 ноября 2011

Хорошо, мой код воспроизводит фильм в полноэкранном режиме без элементов управления для пользователя (в основном кинематографический).После завершения фильма мое NSNotification запускает и загружает представление.

Однако, когда пользователь нажимает кнопку «Домой» во время одного из этих фильмов, он приостанавливается, но нет способа заставить его воспроизводиться снова, так какЯ забрал контроль.Я попытался поместить [playerController play] и [playerController shouldAutoplay] в мой AppDelegate.m в applicationDidBecomeActive, но там он не определен, поэтому он не знает, что такое 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 Ответ

2 голосов
/ 23 ноября 2011

при реализации вашего игрока отправьте все необходимые указатели делегату приложения:

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.
     */
}
...