检查 YouTube 视频 (MPMoviePlayer) 是否结束

发布于 2024-11-05 19:04:17 字数 786 浏览 0 评论 0原文

我有一个带有加载 YouTube 视频的网络视图的视图。我使用以下方法在加载网络视图时自动启动 YouTube 视频。

Web 视图会打开 iPhone 的本机电影播放器​​。有什么方法可以检查视频是否已结束或用户是否已按下电影播放器​​的“确定”按钮并从而关闭播放器?

这些是我用来自动启动网络视图的方法:

- (UIButton *)findButtonInView:(UIView *)view {
    UIButton *button = nil;

    if([view isMemberOfClass:[UIButton class]]) {
        return (UIButton *)view;
    }

    if(view.subviews && [view.subviews count] > 0) {
        for(UIView *subview in view.subviews) {
            button = [self findButtonInView:subview];
            if(button) return button;
        }
    }

    return button;
}

- (void)webViewDidFinishLoad:(UIWebView *)_webView {
    UIButton *b = [self findButtonInView:_webView];
    [b sendActionsForControlEvents:UIControlEventTouchUpInside];
}

I have a view with a web view which loads a YouTube video. I am using the following methods to automatically start the YouTube video when the web view is loaded.

The web view opens the iPhones native movie player. Is there any way to check if the video has ended or the user has pushed the "OK" button of the movie player and the player is thereby closed?

These are the methods I use for automatically starting the web view:

- (UIButton *)findButtonInView:(UIView *)view {
    UIButton *button = nil;

    if([view isMemberOfClass:[UIButton class]]) {
        return (UIButton *)view;
    }

    if(view.subviews && [view.subviews count] > 0) {
        for(UIView *subview in view.subviews) {
            button = [self findButtonInView:subview];
            if(button) return button;
        }
    }

    return button;
}

- (void)webViewDidFinishLoad:(UIWebView *)_webView {
    UIButton *b = [self findButtonInView:_webView];
    [b sendActionsForControlEvents:UIControlEventTouchUpInside];
}

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

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

发布评论

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

评论(1

鹿港小镇 2024-11-12 19:04:17

苹果不会为此推送[记录的]通知,所以你必须采取一些技巧。

我这样做的方法是检查应用程序的 keyWindow。我从这里得到了这个想法。

在您的 .h 文件中,跟踪计时器和所需的 keyWindow:

NSTimer *windowTimer;
UIWindow *keyWindow;

在 .m 文件中,您需要以下内容:

- (void)viewDidLoad {
    [super viewDidUnload];
    keyWindow = [[UIApplication sharedApplication] keyWindow];
}

然后编辑您的委托方法并添加一个新方法:

- (void)webViewDidFinishLoad:(UIWebView *)_webView {
    UIButton *b = [self findButtonInView:_webView];
    [b sendActionsForControlEvents:UIControlEventTouchUpInside];

    // start checking the current keyWindow
    windowTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(checkWindowStatus) userInfo:nil repeats:YES];
}

- (void) checkWindowStatus {
    // if the key window is back to our application
    if (keyWindow == [[UIApplication sharedApplication] keyWindow]) {
        [windowTimer invalidate];
        windowTimer = nil;

        ... window has dismissed, do your thing ...
    }
}

Apple doesn't push a [documented] notification for this, so you have to get a little tricky.

The way I do it is to check the application's keyWindow. I got the idea from here.

in your .h file, keep track of your timer and the desired keyWindow:

NSTimer *windowTimer;
UIWindow *keyWindow;

in your .m file, you need the following:

- (void)viewDidLoad {
    [super viewDidUnload];
    keyWindow = [[UIApplication sharedApplication] keyWindow];
}

Then Edit your delegate method and add one new method:

- (void)webViewDidFinishLoad:(UIWebView *)_webView {
    UIButton *b = [self findButtonInView:_webView];
    [b sendActionsForControlEvents:UIControlEventTouchUpInside];

    // start checking the current keyWindow
    windowTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(checkWindowStatus) userInfo:nil repeats:YES];
}

- (void) checkWindowStatus {
    // if the key window is back to our application
    if (keyWindow == [[UIApplication sharedApplication] keyWindow]) {
        [windowTimer invalidate];
        windowTimer = nil;

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