How to embed YouTube video on iOS and play it directly on UIWebView without full screen?

If anyone is still facing this problem, below is by far the best solution I have seen. Works like a charm.

self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10,300, 200)];
        [self.webView setAllowsInlineMediaPlayback:YES];
        [self.webView setMediaPlaybackRequiresUserAction:NO];

        [self.view addSubview:self.webView];

        NSString* embedHTML = [NSString stringWithFormat:@"\
                               <html>\
                                    <body style="margin:0px;padding:0px;">\
                                        <script type="text/javascript" src="http://www.youtube.com/iframe_api"></script>\
                                        <script type="text/javascript">\
                                            function onYouTubeIframeAPIReady()\
                                            {\
                                                ytplayer=new YT.Player('playerId',{events:{onReady:onPlayerReady}})\
                                            }\
                                            function onPlayerReady(a)\
                                            { \
                                                a.target.playVideo(); \
                                            }\
                                        </script>\
                                        <iframe id='playerId' type="text/html" width="%d" height="%d" src="http://www.youtube.com/embed/%@?enablejsapi=1&rel=0&playsinline=1&autoplay=1" frameborder="0">\
                                    </body>\
                               </html>", 300, 200, @"JW5meKfy3fY"];
        [self.webView loadHTMLString:embedHTML baseURL:[[NSBundle mainBundle] resourceURL]];

Source: https://code.google.com/p/gdata-issues/issues/detail?id=5204

Leave a Comment