Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Playing youtube video in UIWebView only using iFrame

I'm trying to play youtube videos on my UIWebView using <iframe> tag with below code -

NSString *html = @"\
<html><head>\
<style type=\"text/css\">\
body {    background-color: transparent;\
color: white; \
}\
</style>\
</head><body style=\"margin:0\">\
<iframe class=\"youtube-player\" width=\"300\" height=\"300\" src=\"http://www.youtube.com/embed/efRNKkmWdc0\" frameborder=\"0\" allowfullscreen=\"true\"></iframe>\
</body></html>";

UIWebView *videoView = [[UIWebView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 460.0)];
[videoView loadHTMLString:html baseURL:nil];

Its working fine.

enter image description here

But, i need to play this on webview itself not using iPhone default player. How do i restrict this to play on UIWebView only. I was defined the iframe player class as class=\"youtube-player\" also. But, its taking iPhone's default player.

Any idea appreciated!

like image 876
Praveenkumar Avatar asked Oct 21 '22 11:10

Praveenkumar


1 Answers

Try using this code to initialize the iframe player:

        player = new YT.Player('player', {
                               height: '100%',
                               width: '100%',
                               videoId: 'M7lc1UVf-VE',
                               playerVars: {
                                 'controls' : '0',
                                 'playsinline' : 1
                               }
                               });

Note the "playsinline" parameter being set to 1. In the containing view, initialize the UIWebView with this parameter:

webView.allowsInlineMediaPlayback = YES;

One thing I've noticed is that you cannot set the baseURL parameter to NULL; it has to be an NSString. Here's an example that loads an HTML file named iframe-player.html containing the required HTML and JavaScript code:

NSString *path = [[NSBundle mainBundle] pathForResource: @"iframe-player" ofType: @"html"];
NSString *embedHTML = [NSString stringWithContentsOfFile: path
                                                encoding:NSUTF8StringEncoding
                                                   error: &error];

[webView loadHTMLString:embedHTML baseURL:[NSURL URLWithString:@"about:blank"]];
like image 60
Ikai Lan Avatar answered Oct 27 '22 19:10

Ikai Lan