在 applicationWillResignActive 时暂停视频并在 applicationDidBecomeActive 时播放视频
好的,我的代码全屏播放电影,无需用户控制(基本上是电影)。电影结束后,我的 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;
Okay, my code plays a movie fullscreen with no controls for the user, (basically a cinematic). After the movie is done my NSNotification fires and loads a view.
However, when the user hits the home button during one of these movies, it pauses, but there is no way to get it to play again, since I took away the controls. I tried putting [playerController play] and [playerController shouldAutoplay] in my AppDelegate.m under applicationDidBecomeActive, but it's not defined there so it doesn't know what playerController is.
Can anyone help me properly pause and play this video if a user gets a text or hits the home button?
-(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];
EDIT:
Header
//ViewController.h
@interface ViewController : UIViewController {
MPMoviePlayerController *playerController;
}
Implementation
//ViewController.m
playerController = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:movieUrl]];
AppDelegate *sharedAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
sharedAppDelegate.pointer = playerController;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实现播放器时,将所有必要的指针发送到应用程序委托:
使用 appDelegate.m 中的此方法:
when implementing your player, send all necessary pointers to app delegate:
use this methomd from appDelegate.m: