2011-05-07 121 views
1

我有加载YouTube视频的网络视图。我正在使用以下方法在Web视图加载时自动启动YouTube视频。检查YouTube视频(MPMoviePlayer)是否已结束

Web视图打开iPhone本地电影播放器​​。有什么方法可以检查视频是否已经结束,或者用户是否按下了电影播放器​​的“确定”按钮,并且播放器因此被关闭?

这些都是我用自动启动Web视图的方法:

- (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]; 
} 

回答

3

苹果不会推[记录]通知这一点,所以你必须得有点棘手。

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

在.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 ... 
    } 
} 
+0

这非常聪明。真棒!这正是我需要的。非常感谢你! – simonbs 2011-05-14 08:24:17