Play youtube video in UIWebView using iFrame only

I am trying to play youtube video on mine UIWebViewusing a tag <iframe>using the code below -

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];

It works well.

enter image description here

But I need to reproduce this on the website itself, not using the iPhone by default. How can I limit this to just playing only on UIWebView. I also defined the player class iframeas class=\"youtube-player\". But, it takes the iPhone player by default.

Any idea appreciated!

+5
source share
2 answers

Try using this code to initialize an iframe player:

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

, "playinline" 1. UIWebView :

webView.allowsInlineMediaPlayback = YES;

, , , baseURL NULL; NSString. , HTML iframe-player.html, HTML JavaScript:

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"]];
+3

. - iPhone. - MPMoviePlayer .

0

All Articles