How to change embed video size while playing in UIWebView? - ios

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

Related

How do I play YouTube videos with iOS-GTLYouTube library?

I'm trying to play YouTube videos using the iOS-GTLYouTube library. I just finished the query code for my videos and got back a few GTLYouTubeVideo objects. So after I got them, then what? How do I play it? I've searched the web but found nothing.
You can't play videos with the GTLYouTube library, you can only use it to search the API for video information.
After retrieving the Video ID of the video you want to play, you can load that id into the iFrame up in a UIWebView.
NSString *videoID = #"HCdUubr70i8"; // the video ID returned from your API query
// Adjust the width and height to your liking, I just pass in the view's frame width and height
NSString *str = [NSString stringWithFormat:#"<html><body style='margin:0px;padding:0px;'><script type='text/javascript' src='http://www.youtube.com/iframe_api'></script><script type='text/javascript'>function onYouTubeIframeAPIReady(){ytplayer=new YT.Player('playerId',{events:{onReady:onPlayerReady}})}function onPlayerReady(a){a.target.playVideo();}</script><iframe id='playerId' type='text/html' width='%#' height='%#' src='http://www.youtube.com/embed/%#?enablejsapi=1&rel=0&playsinline=1&autoplay=1' frameborder='0'></body></html>", self.view.frame.size.width, self.view.frame.size.height, videoID];
UIWebView *webView = [UIWebView ...];
[webView loadHTMLString:embedHTML baseURL: [[NSBundle mainBundle] bundleURL]];
Google also provides their own UIWebView wrapper class, youtube-ios-player-helper, which makes YouTube video playback on iOS devices a little easier.

UIWebView youtube iframe api autoplay/playsinline quit working in iOS7

This code has worked really well to auto-play video inline in an iOS 6 uiwebview. However, I upgraded to iOS 7 and now my video will not auto-play. Sometimes video will auto-play and sometimes it won't. It will auto-play about 10% of the time. Most of the time I get a spinning wheel. If I remove &playsinline=1 from the src tag it will auto-play fullscreen (not what I want). I spent a lot of time creating a custom player for this app and now it won't work correctly. Any help is greatly appreciated.
_youTubeWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 20, 320, 184)];
_youTubeWebView.delegate = self;
_youTubeWebView.scrollView.bounces = NO;
_youTubeWebView.allowsInlineMediaPlayback = YES;
_youTubeWebView.mediaPlaybackAllowsAirPlay = YES;
_youTubeWebView.mediaPlaybackRequiresUserAction = NO;
[self.view addSubview:_youTubeWebView];
NSString* embedHTML = [NSString stringWithFormat:#"<html><body style='margin:0px;padding:0px;'><script type='text/javascript' src='http://www.youtube.com/iframe_api'></script><script type='text/javascript'>function onYouTubeIframeAPIReady(){ytplayer=new YT.Player('playerId',{events:{onReady:onPlayerReady}})}function onPlayerReady(a){ a.target.playVideo(); }</script><iframe id='playerId' type='text/html' width='%#' height='%#'src='http://www.youtube.com/embed/%#?enablejsapi=1&rel=0&playsinline=1&controls=0&showinfo=0' frameborder='0'></body></html>", w, h, videoId];
[_youTubeWebView loadHTMLString:embedHTML baseURL:nil];
Here's a link to a quick test file. videoPlayerTest.zip
This sounds like the following open bug against the Youtube iframe player API:
Issue 5204: js iframe api playVideo() doesn't play video in iOS 7
http://code.google.com/p/gdata-issues/issues/detail?id=5204
You can fix the autoplay behavior by changing the baseURL to:
[[NSBundle mainBundle] resourceURL]

How can I embed a Youtube video in a iOS6 app?

I have problems embedding Youtube videos in a iOS6 app.
The problem is that whenever I create a html element that uses the old / or new embed code, it opens Safari and shows the video
I use the Youtube API to get videos, and use PhoneGap
Update
The solution is to set OpenAllWhitelistURLsInWebView to YES in Phonegap settings
You can use UIWebview to load videos instead of Safari.
Here is a sample code
CGRect rect = [[UIScreen mainScreen] bounds];
CGSize screenSize = rect.size;
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,screenSize.width,screenSize.height)];
webView.autoresizesSubviews = YES;
webView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
NSString *videoUrl = #"http://www.youtube.com/v/oHg5SJYRHA0";
NSString *htmlString = [NSString stringWithFormat:#"<html><head><meta name = \"viewport\" content = \"initial-scale = 1.0, user-scalable = no, width = 212\"/></head><body style=\"background:#F00;margin-top:0px;margin-left:0px\"><div><object width=\"320\" height=\"480\"><param name=\"movie\" value=\"%#\"></param><param name=\"wmode\" value=\"transparent\"></param><embed src=\"%#\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"320\" height=\"480\"></embed></object></div></body></html>",videoUrl,videoUrl] ;
[webView loadHTMLString:htmlString baseURL:[NSURL URLWithString:#"http://www.youtube.com"]];
[window addSubview:webView];
[webView release];
Not shure if you are talking a real APP (dont know anything about that)
but if its a WEBAPP (a normal webpage that tries to look as a real APP),
Then the only way i found to prevent youtube-videos from opening in Safari/Qt-player
is to embed it using "Fancybox" (maybe other boxes also).
that will keep it on the page in Youtube´s own player.
You can even have a original playlistplayer.
I stopped trying to embed youtube videos on iPhone-pages (to small)
I tried to include a link to a jSfiddle-example but this forum wont accept it
it forces me to write the whole example here...which i´m not going to do
because that is to much work for me right now.
(dont know how others are allowed to link to jSfiddle)
This forum is way to dictative to my taste.
Anyway you should be able to (incase of a webapp) find info at Fancybox´s website.

Embed Youtube videos :- with contains content from * , it is restricted from playback on certain site

How to embed a Youtube video that has copyrighted content in it.
For example when you try playing this video(http://www.youtube.com/embed/ADBKdSCbmiM) in a UIWebView it says
This Video Contains content from Vevo. It is restricted from playback on certain sites
How would I go about getting videos like this to play in an embedded player or a custom player that I would make using MPMoviePlayer or such. I know this is possible as following app does this. (http://itunes.apple.com/us/app/audioviz-view-your-songs-on/id547001249?mt=8)
Edit
Few videos play in browser but in iOS Simulator its showing
This video contains content from SME . It is restricted from playback on certain site
Thanks for all the help!
You can use player vars while initializing youtube sdk:
NSDictionary *playerVars = #{
#"origin" : #"http://www.youtube.com",
};
[self.playerView loadWithVideoId:#"KOvoD1upTxM" playerVars:playerVars];
Enjoy!
To play the youtube video using uiwebview:
First check if your youtube URL is played or not in browser. If the video doesn't play in the browser it won't play in the device either
You only check in the device; the video doesn't play in simulator
- (void) displayGoogleVideo
{
CGRect rect = [[UIScreen mainScreen] bounds];
CGSize screenSize = rect.size;
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,screenSize.width,screenSize.height)];
webView.autoresizesSubviews = YES;
webView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
NSString *videoUrl = #"http://www.youtube.com/v/oHg5SJYRHA0"; // valid youtube url
NSString *htmlString = [NSString stringWithFormat:#"<html><head><meta name = \"viewport\" content = \"initial-scale = 1.0, user-scalable = no, width = 212\"/></head><body style=\"background:#F00;margin-top:0px;margin-left:0px\"><div><object width=\"320\" height=\"480\"><param name=\"movie\" value=\"%#\"></param><param name=\"wmode\" value=\"transparent\"></param><embed src=\"%#\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"320\" height=\"480\"></embed></object></div></body></html>",videoUrl,videoUrl] ;
[webView loadHTMLString:htmlString baseURL:[NSURL URLWithString:#"http://www.youtube.com"]];
[window addSubview:webView];
[webView release];
}
If you're embedding on a mobile app, you need to set your Referer in your request header for the video. Otherwise, Youtube automatically blacklists you if your Referer field is left blank.
I answered this in detail here. I was experiencing this myself and I fixed it in my case (iOS app using React Native). I can play Vevo, WMG, and all other restricted content now.

After playing a youtube-video in an UIWebView, the app restarts

i'm using the following code, so load a Youtube video in my UIWebView (Outlet):
NSString *videoUrl = [self getVideoURL];
NSString *htmlString = [NSString stringWithFormat:#"<html><head><meta name = \"viewport\" content = \"initial-scale = 1.0, user-scalable = no, width = 280\"/></head><body style=\"background:#F00;margin-top:0px;margin-left:0px\"><div><object width=\"280\" height=\"156\"><param name=\"movie\" value=\"%#\"></param><param name=\"wmode\" value=\"transparent\"></param><embed src=\"%#\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"280\" height=\"156\"></embed></object></div></body></html>",videoUrl,videoUrl] ;
[videoView loadHTMLString:htmlString baseURL:[NSURL URLWithString:#"http://www.youtube.com"]];
all this works fine. but after watching the video and tapping on "done", my app starts new from beginning... but i don't understand why.
hope someone can give me a hint why this is happening?
ok I found my mistake:
the "bug" is at the youtube player. After pressing "done", it returns to my rootView Controller. In my case, this is the really first view of the app.
I solved this by setting my ModalView as my rootViewController after presenting it.

Resources