As for the authors of other answers, it is quite possible to do this using UIWebViews in iOS and without using private APIs. Here is a pretty simple way to do this.
- (void)playYoutubeVideo:(NSString *)videoId { UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectZero]; webView.center = self.view.center; //Technically not needed, I've found it looks better with this added (when the video is done playing it looks better while being minimized) webView.mediaPlaybackRequiresUserAction = NO; [self.view addSubview:webView]; NSString *youTubeVideoHTML = [NSString stringWithFormat:@"<html><head><style>body{margin:0px 0px 0px 0px;}</style></head> <body> <div id=\"player\"></div> <script> var tag = document.createElement('script'); tag.src = 'http://www.youtube.com/player_api'; var firstScriptTag = document.getElementsByTagName('script')[0]; firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); var player; function onYouTubePlayerAPIReady() { player = new YT.Player('player', { width:'768', height:'1024', videoId:'%@', events: { 'onReady': onPlayerReady } }); } function onPlayerReady(event) { event.target.playVideo(); } </script> </body> </html>", videoId]; [webView loadHTMLString:youTubeVideoHTML baseURL: [NSBundle mainBundle].bundleURL]; }
The essence of this implementation is to use a UIWebView with a CGRectZero frame along with custom HTML / Javascript that handles auto-play of video. With a UIWebView with a CGRectZero frame, the user does not see any annoying intermediate page before the video is automatically played, and HTML / Javascript makes a heavy lift related to getting the page to automatically play the video.
There is a slight delay before the video is presented. I did not find a way to eliminate this pair of second delay, unfortunately.
Tyler hunnefeld
source share