Embed youtube video in IOS8 does not fill up Webview width 100% - webview

I have used following code for showing youtube video in iOS.
//Html string to display embed youtube video in native webview
NSString *embedHTML = #"\
<html><head>\
<style type=\"text/css\">\
body {\
background-color: transparent; background:none;\
}\
</style>\
</head><body style=\"margin:0\">\
<embed src=\"%#\"\
type=\"application/x-shockwave-flash\" width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";
NSString* html = [NSString stringWithFormat:embedHTML, http,viewRect.size.width, (viewRect.size.height*-1)];
videoView = [[UIWebView alloc] initWithFrame:CGRectMake(viewRect.origin.x , (viewRect.origin.y + viewRect.size.height), viewRect.size.width, (viewRect.size.height*-1))];
videoView.delegate = self;
//Stop Image Move
[videoView.scrollView setScrollEnabled:NO];
[self addSubview:videoView];
[videoView loadHTMLString:html baseURL:nil];
videoView.opaque = NO;
[self bringSubviewToFront:videoView];
I have assigned same width and height to both web view and the embed tag.Previously in iOS7, this code works well without any problem. But in iOS8 the youtube video width doesn't fills up the web view.Please I will really be a help, if someone advice on this issue.
Thanks in advance.

Related

How to change embed video size while playing in UIWebView?

I have a UIWebView with size 250x160, which should play video from youtube.com.
NSString *videoURL = #"http://www.youtube.com/v/Y4Y_a45Bv20?version=3&f=videos&app=youtube_gdata";
NSString *videoHTML = [NSString stringWithFormat:#"\
<html><body>\
<embed src=\"%#\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"%f\" height=\"%f\">\
</embed>\
</body></html>", videoURL, self.web.frame.size.width-20, self.web.frame.size.height-20];
[self.web loadHTMLString: videoHTML baseURL: nil];
While playing video I want to resize the UIWebView. When I make it, for example 2 times bigger, embed video stays how it was 250x160. How can I resize it without restarting it?
I found anoyingly easy solution :
NSString *videoHTML = [NSString stringWithFormat:#"<iframe class=\"youtube-player\" type=\"text/html\" width=\"100%%\" height=\"98%%\" src=\"http://www.youtube.com/embed/Y4Y_a45Bv20?showinfo=0\" frameborder=\"0\"></iframe>"];
The key is in width=\"100%%\" and height=\"98%%\". I didn't set height to 100%%, because while playing video, it's strangely increases and after 10-20 seconds you need to scroll uiwebview to find where video is gone.
And don't forget set UIWebView scalePageToFit = YES;
try affineTransform.
webview.transform = CGAffineTransformMakeScale(2, 2);
or
webView.scalesPageToFit=YES;
I found this approach and it worked for me:
webView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
Auto-resize iOS WebView Helper

YouTube Embed player in Iframe doesn't work in iOS6

I really need your help. Working on an iOS app. I want to play youtube. I understood from reading many many blogs and posts, that we need to use an iframe in order to play a youtube video.
However, on some videos I get: "This video contains content from XYZ. It is restricted from playback on certain sites. Watch on YouTube"
I read this question: Youtube in iOS5 - done button Tapped which gives the link to youtube api: https://developers.google.com/youtube/player_parameters
They recommend to use the iframe. the example for youtube site is:
<iframe id="ytplayer" type="text/html" width="640" height="390"
src="http://www.youtube.com/embed/M7lc1UVf-VE?autoplay=1&origin=http://example.com"
frameborder="0"/>
The code I used:
<!DOCTYPE html>
<html>
<head>
<style>
* {
border:0;
margin:0;
}
</style>
</head>
<body>
<iframe webkit-playsinline id="player" type="text/html" width="320" height="180" src="http://www.youtube.com/embed/rEevIL1Wpcg?enablejsapi=1&playsinline=1&autoplay=1" frameborder="0">
</iframe>
</body>
</html>
Can someone help me understand it? I check the embedded flag to be true, they are all clips that are allowed to play on mobile devices.
Example for videos that works on the device:
http://www.youtube.com/watch?v=rEevIL1Wpcg&feature=youtube_gdata
http://www.youtube.com/watch?v=KzGe7pbGUiM
Example for videos that don't work on the device and bring up the error msg:
http://www.youtube.com/watch?v=1vhFnTjia_I&feature=youtube_gdata
http://www.youtube.com/watch?v=sxkiA0IjBZ0&feature=youtube_gdata
you can use webview as youtube player
Try Below Code it is working for me
in .h file
#property (strong, nonatomic) UIWebView *webView;
and in your .m file
NSString *videoURL = #"http://www.youtube.com/embed/M7lc1UVf-VE?autoplay=1&origin=http://example.com";
// if your url is not in embed format or it is dynamic then you have to convert it in embed format.
videoURL = [videoURL stringByReplacingOccurrencesOfString:#"watch?v=" withString:#"embed/"];
NSRange range = [videoURLString rangeOfString:#"&"];
#try {
videoURLString = [videoURLString substringToIndex:range.location];
}
#catch (NSException *exception) {
}
// here your link is converted in embed format.
NSString* embedHTML = [NSString stringWithFormat:#"\
<html><head>\
<style type=\"text/css\">\
iframe {position:absolute; top:50%%; margin-top:-130px;}\
body {\
background-color: transparent;\
color: white;\
}\
</style>\
</head><body style=\"margin:0\">\
<iframe width=\"100%%\" height=\"240px\" src=\"%#\" frameborder=\"0\" allowfullscreen></iframe>\
</body></html>",videoURL];
self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
[self.view addSubview:self.webView];
[self.webView loadHTMLString:embedHTML baseURL:nil];
Here you can change webview frame as you want and also can change videoUrl.
There are two concepts, embeddable and syndicated.
iOS devices use iframe so they basically embed.
Android devices that use player API can check syndicated.
When you do a search->list, you can set videoEmbeddable and videoSyndicated to true.
Or if you are iterating through videos, for each video, you can do a video->list call with video id and check status.embeddable in the response.
Here is a blog post about this topic, even though examples are in v2, information is still relevant.

How to access ios media player instance when youtube video is playing

I used below function for play youtube videos in iPhone using UIWebView
- (void)embedYouTube {
videoHTML = [NSString stringWithFormat:#"\
<html>\
<head>\
<style type=\"text/css\">\
iframe {position:absolute; top:50%%; margin-top:-130px;}\
body {background-color:#000; margin:0;}\
</style>\
</head>\
<body>\
<iframe width=\"560\" height=\"315\" src=\"http://www.youtube.com/embed/xxxxxxxxxxxxx\" frameborder=\"0\" allowfullscreen></iframe>\
</body>\
</html>"];
[videoView loadHTMLString:videoHTML baseURL:nil];
}
By this method video will play in the iOS media player. I wanted to hide controllers automatically, when the video is playing. Problem is how to access the media player instance.
I think you can try Subview concept, to access MoviePlayercontroller. Please refer this article here.

Play youtube videos-ipad

In my application i want to play the Youtube URL(ipad).I'm using the below code.It simply show me a White screen
Youtube url is:http://www.youtube.com/watch?v=x61GMbKwMkE
This is my code:
NSString *embed = #"\
<html><head>\
<style type=\"text/css\">\
body {\
background-color: transparent;\
color: white;\
}\
</style>\
</head><body style=\"margin:0\">\
<embed id=\"yt\" src=\"%#\" type=\"application/x-shockwave-flash\" \
width=320 height=460></embed>\
</body></html>";
NSString *embedURL=[NSString stringWithFormat:embed,appDelegate.videoURL];
NSLog(#"\n\n\n\n Myurl==%# %#\n\n\n",embedURL,appDelegate.videoURL);
[[WebView.subviews objectAtIndex:0] setScrollEnabled:NO];
[WebView loadHTMLString:embedURL baseURL:nil];
I'm creating and connecting UIWebView in XIB.
Anyone Guide me to do this
Thanks For your consideration and effort
Flash is not supported in iOS. Check out the new embed code for YouTube videos that will automatically choose the correct player for each environment.

Video in UIView (or UIWebView)

I would like to know if I can play a video in UIView?
I want to play a video in a small UIView (say 100 x 100 ) which should be played automatically when the UIView loads.
First I would say what I tried out ... It would be really helpful if the experts ( I am a 2 week old baby in IOS development & Obj- C) to confirm whether the route I am heading is right..
I created a UIWebView and and tried to embed a video in it using HTML video tag (html5)
For the UIWebview I set the allowInlineMediaPlayback to YES and mediaPlaybackRequiresUserAction to NO ... Also in the HTML code, for the video tag, I used - webkit-playsinline.
Any help is much appreciated.
Update
This is my code with my html block ... I did add the play video JS .... But when I run the app in simulator , I do not get the video , only get audio.
NSString *embedHTML = #"\
<!DOCTYPE HTML>\
<html>\
<head>\
<script type=\"text/javascript\">\
function playvideo() {\
var myVideo=document.getElementsByTagName('video')[0];\
myVideo.play();\
myVideo.webkitExitFullscreen();\
myVideo.height = 120;\
myVideo.width = 120;\
}\
</script>\
</head>\
<body onLoad = \"playvideo()\" >\
<video id=\"player\" webkit-playsinline width=\"140\" height=\"140\">\
<source src=\"file:///Users/Shilpa/Desktop/Movie Player/MoviePlayer-ios4-Next/Movie-1.mp4\"/>\
</video>\
</body>\
</html>";
webView = [[UIWebView alloc] initWithFrame:CGRectMake(100.0, 170.0, 150.0, 150.0)];
[webView setOpaque:YES];
NSString *html = [NSString stringWithFormat:embedHTML, webView.frame.size.width, webView.frame.size.height];
[webView loadHTMLString:html baseURL:nil];
webView.allowsInlineMediaPlayback = YES;
webView.mediaPlaybackRequiresUserAction = NO;
webView.transform = CGAffineTransformMakeRotation(3.14/2);
[moviePlayerWindow addSubview:webView];
HTML Video tag, might needs an action via JavaScript,
try to put following code in your html page,
<body onload = playvideo() >
and see the magic :)
i believe, only to play the video you are using WebView, but you can use the QTView also to do the same.

Resources