在 applicationWillResignActive 时暂停视频并在 applicationDidBecomeActive 时播放视频

发布于 2024-12-17 08:17:12 字数 2070 浏览 3 评论 0原文

好的,我的代码全屏播放电影,无需用户控制(基本上是电影)。电影结束后,我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

任性一次 2024-12-24 08:17:12

实现播放器时,将所有必要的指针发送到应用程序委托:

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

when implementing your player, send all necessary pointers to app delegate:

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

use this methomd from 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.
     */
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文