Alejandro Luengo Alejandro Luengo - 1 year ago 142
iOS Question

How to embed YouTube video on iOs and play it directly on UIWebview without full screen

I am using this code to play YouTube videos on iOS

- (void)embedYouTube:(NSString *)urlString frame:(CGRect)frame
NSString *htmlString = [NSString stringWithFormat:@"<html><head><meta name = \"viewport\" content = \"initial-scale = 1.0, user-scalable = yes, width = 320\"/></head><body style=\"background:#00;margin-top:0px;margin-left:0px\"><div><object width=\"320\" height=\"180\"><param name=\"movie\" value=\"\"></param><param name=\"wmode\" value=\"transparent\"></param><embed src=\"\"type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"320\" height=\"180\"></embed></object></div></body></html>", urlString, urlString];

UIWebView *videoView = [[UIWebView alloc] initWithFrame:frame];
[videoView loadHTMLString:htmlString baseURL:nil];
[self.view addSubview:videoView];
[videoView release];


It works like charm but I want a different behaviour. Now a thumbnail of the video appears on the webview (nice!) but when I tap the play icon it opens in full screen. I need that playback is done in the same window since I need to show more stuff.

Any clue of how making that? Thansk in advance

Answer Source

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:@"\
                                    <body style='margin:0px;padding:0px;'>\
                                        <script type='text/javascript' src=''></script>\
                                        <script type='text/javascript'>\
                                            function onYouTubeIframeAPIReady()\
                                                ytplayer=new YT.Player('playerId',{events:{onReady:onPlayerReady}})\
                                            function onPlayerReady(a)\
                                            { \
                                      ; \
                                        <iframe id='playerId' type='text/html' width='%d' height='%d' src='' frameborder='0'>\
                               </html>", 300, 200, @"JW5meKfy3fY"];
        [self.webView loadHTMLString:embedHTML baseURL:[[NSBundle mainBundle] resourceURL]];


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download