Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Youtube video autoplay on iPhone's Safari or UIWebView

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?

like image 984
Zan Avatar asked Jun 09 '10 23:06

Zan


People also ask

How do I turn off video autoplay in Safari on iPhone?

How to stop videos from automatically playing on iPhone. Open Settings. Tap Accessibility → Motion. Toggle off Auto-Play Video Previews.

Why are videos automatically playing on Safari?

If your iPhone is running the latest version of iOS 13, the setting to disable auto-playing videos is in your Accessibility settings. You can turn off the auto-play setting to prevent videos from auto-playing in any of the native Apple apps, including Safari.


3 Answers

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];
}
like image 58
Saurabh Wadhwa Avatar answered Oct 23 '22 00:10

Saurabh Wadhwa


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.

like image 26
Rob Avatar answered Oct 23 '22 00:10

Rob


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.

like image 39
chrux Avatar answered Oct 22 '22 22:10

chrux