开发者

How can I start a YouTube video without the user touching the UIWebView itself?

We're developing an app which contains a TableView with some standard cells, but also a list of YouTube videos. We know how to embed the YouTube video data into a UIWebView and play it when the user touches it. But in order to do that, users must touch the UIWebView.

Since it's a TableView, every cell is the typical cell with the thumbnail on the left, some info and an accessory bu开发者_Python百科tton on the right. We want to let users touch anywhere in the cell, not just the WebView (didSelectRow) so the video would start.

How can we achieve this?

  1. We've thought of enabling a WebView at didSelectRow, embed the HTML data and show it fullscreen, but with this way users would need to touch once again into the play button to play the video, am I right?
  2. Would it be possible to use MPMoviePlayerController instead, setting as the request URL the flv path of the video? (any api suggested?)
  3. The last one would be simulating the touch inside the WebView so it would start the video, even if the user hasn't touched the UIWebView. Is it possible?
  4. Any other suggestions?


Answering myself. I've found the solution. This code will autoplay the webview.

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

Youtube video autoplay on iPhone's Safari or UIWebView

Hope it helps somebody else.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜