19

Is it possible to get a youtube video to autoplay on Safari and/or UIWebView? I've seen this done in an iPhone app, the tableview displays cells that do not have Youtube preview icon (pretty sure it's a UIWebView Though), when you tap the cell it directly goes to video.

Could this be done by faking a tap on the youtube video? If so, how? Would getElementById().click work?

alex
  • 438,662
  • 188
  • 837
  • 957
Zan
  • 301
  • 1
  • 2
  • 5

5 Answers5

30

the trick is to find the button in the webview. Use the following snippet.

- (void)loadWebUIViewWithYoutubeLink {
    webView.delegate = self;
    NSString *htmlString = [NSString stringWithFormat:[NSString
                             stringWithContentsOfFile:[[NSBundle mainBundle]
        pathForResource:@"YouTubeTemplate" ofType:@"txt"]],
                             @"b85hn8rJvgw",  @"b85hn8rJvgw", nil];
    [webView loadHTMLString:htmlString baseURL:[NSURL URLWithString:@"http://youtube.com"]];
}

- (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];
}
Saurabh Wadhwa
  • 1,173
  • 12
  • 16
  • The template which works for me: NSString *template = @""; NSString* html = [NSString stringWithFormat:template, feedURL, 480.0, 289.0]; –  Aug 28 '11 at 18:46
  • The entire second if statement is unnecessary - it is perfectly safe to do fast enumeration on a nil or 0 object array. The code is logically equivalent without the 2nd if statement. – charliehorse55 Jul 09 '12 at 19:01
7

If you create your own html file that uses an iframe and then you load the webview to that then you can just put in the command

webView.mediaPlaybackRequiresUserAction=FALSE; 

and the video will autoplay as long as you have already had your youtube video in the html already start the video. The html file can be found on the source of the following page. view-source:http://tylerbiscoe.com/vb/iphone_player.html. If you copy and paste that into the browser you can see what the iframe page will look like. Then use the webView to load your youTube page and then use the command above to get the quicktime player that gets called by ios to auto play after the webview is done loading. The page above has three videos already included and all three will play in series to one another.

Rob
  • 426
  • 8
  • 22
2

Yes, It works (all comments here) but you have to use youtube old embed src.

Like this:

http://www.youtube.com/v/%@?version=3&hl=en_US

Where %@ is the youtube video ID.

chrux
  • 31
  • 5
0

I used Saurabh's method but instead of loading a HTML string I was able to just load a normal request and the video still auto-played.

Be sure to set the delegate to nil when you close the webview though or else it will keep trying to play.

- (void)loadVideo
{
        NSURLRequest *videoReq = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.youtube.com/watch?v=J-30wYSsFUY&feature=plcp"]];
        [_videoWebView setDelegate:self];
        [_videoWebView loadRequest:videoReq];
}

// -----THESE 2 FUNCTIONS TAKEN FROM Saurabh Wadhwa's ANSWER ------

- (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];
}
RyanG
  • 4,113
  • 1
  • 34
  • 60
0

The "button click" method mentioned above worked for me in iOS6, but only the first time. Upon repeat loads, it would not start the video. All things equal, I figured it must be a caching issue, or a cookie. Turns out clearing the cookies for youtube made it work for me.

for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
      NSLog([cookie domain]);
      if([[cookie domain] isEqualToString:@".youtube.com"]) {

          [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
      }
}

Hope this can help someone else trying to figure this out for iOS6!

Jameo
  • 4,186
  • 8
  • 34
  • 65