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.
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!
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"]];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With