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

匆匆过客 提交于 2019-11-29 15:29:24

问题


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 button 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?

回答1:


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.



来源:https://stackoverflow.com/questions/5510429/how-can-i-start-a-youtube-video-without-the-user-touching-the-uiwebview-itself

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!