开发者

Youtube video autoplay on iPhone's Safari or UIWebView

开发者 https://www.devze.com 2023-01-03 10:15 出处:网络
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\

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 UIWeb开发者_如何学PythonView 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?


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


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.


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.


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


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!

0

精彩评论

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