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?
How to stop videos from automatically playing on iPhone. Open Settings. Tap Accessibility → Motion. Toggle off Auto-Play Video Previews.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With