MPMoviePlayer 完成按钮问题

发布于 2024-11-10 02:26:45 字数 117 浏览 1 评论 0原文

我正在使用 MPMoviePlayer 来显示视频。我进入全屏模式,当单击“完成”按钮时,我希望它从我的视图中删除整个电影播放器​​。目前它仅退出全屏模式。你如何跟踪被点击的doneButton或者我该如何解决这个问题?

I am using a MPMoviePlayer to display a video. I go into full screen and when the done button is clicked I want it to remove of the entire movie player from my view. Currently it only goes out of the fullscreen mode. How do you track the doneButton being clicked or just how do I go about fixing this issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

荒人说梦 2024-11-17 02:26:45

您可以通过在 MPMoviePlayerDidExitFullscreenNotification 上添加通知处理程序来实现此目的,因为一旦用户点击“完成”按钮,该通知就会发送。

现在在初始化程序中的某个位置

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MPMoviePlayerDidExitFullscreen:) name:MPMoviePlayerDidExitFullscreenNotification object:nil];

实现该处理程序:

- (void)MPMoviePlayerDidExitFullscreen:(NSNotification *)notification
{
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerDidExitFullscreenNotification 
                                                  object:nil];

    [moviePlayerController stop];
    [moviePlayerController.view removeFromSuperview];
}

You can do that by adding a notification handler on MPMoviePlayerDidExitFullscreenNotification as that notification gets sent once the user taps on the DONE Button.

Somewhere in your initializer

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MPMoviePlayerDidExitFullscreen:) name:MPMoviePlayerDidExitFullscreenNotification object:nil];

Now implement that handler:

- (void)MPMoviePlayerDidExitFullscreen:(NSNotification *)notification
{
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerDidExitFullscreenNotification 
                                                  object:nil];

    [moviePlayerController stop];
    [moviePlayerController.view removeFromSuperview];
}
梦里泪两行 2024-11-17 02:26:45

据我所知,单击“完成”按钮时您不会收到通知。但是,单击“完成”按钮后,当电影播放器​​退出全屏时,您会收到通知。为此,您可以使用 MPMoviePlayerDidExitFullscreenNotification

要观察此通知并采取行动,您需要将以下代码粘贴到包含 IBAction 的类文件中(将其放入 viewDidLoad 方法中):

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(exitedFullScreen) name:@"MPMoviePlayerDidExitFullscreenNotification" object:nil];

现在您需要在同一个类中创建 exitedFullScreen 方法:

-(void) exitedFullScreen
{
     //Do whatever you want here
}

最后,在您的 viewDidUnload 方法中,粘贴以下行:

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"MPMoviePlayerDidExitFullscreenNotification" object:nil];

解释发生了什么:

“addObserver”代码行在 viewDidLoad 中确保负责处理 moviePlayer 的 viewController 正在监听 MPMoviePlayerDidExitFullScreen 通知。

该行使得当通知到来时, exitedFullScreen 方法被触发,您可以在其中放置单击“完成”按钮时想要运行的代码。

在 viewDidUnload 中,viewController 将被卸载,因此您想要停止监听通知,因此需要删除Observer 部分。

To the best of my knowledge, you can't be notified when the Done button is clicked. You can, however be notified when the movie player exits fullscreen after the Done button is clicked. For this, you use the MPMoviePlayerDidExitFullscreenNotification

To observe and act upon this notification you need to paste the following code in your class file which contains the IBAction (put it in the viewDidLoad method):

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(exitedFullScreen) name:@"MPMoviePlayerDidExitFullscreenNotification" object:nil];

Now you need to create the exitedFullScreen method in the same class:

-(void) exitedFullScreen
{
     //Do whatever you want here
}

Finally, in your viewDidUnload method, paste the following line:

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"MPMoviePlayerDidExitFullscreenNotification" object:nil];

To explain what's going on:

The "addObserver" line of code in your viewDidLoad makes sure your viewController responsible for handling the moviePlayer is listening to the MPMoviePlayerDidExitFullScreen notification.

That line makes it so that when the notification comes is, the exitedFullScreen method is fired off, where you would put the code you wanted to run when the Done button was clicked.

In the viewDidUnload, the viewController is going to be unloaded so you want to stop listening to the notification, hence the removeObserver part.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文