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

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.

Related

How to get a video to play in UIWebView (video is in a ALAsset group)?

Okay maybe the headline is somehow disturbing, I hope to state this so you can understand my problem.
So I have a scrollview which has paging enabled. I want to build something like iOS Photo App.
I load the photos and videos which are on the device via ALAsset and store them in a group.
Now i want to present each object on a separate subview of the scrollview.
For the pictures it is working fine. But for videos I cant make it work.
Here is what i did and what did not work:
First:
UIWebView *webView = [[UIWebView alloc] initWithFrame:frame];
NSString *embedHTML = [NSString stringWithFormat: #"<html><head><style></style></head><body>\
<video id='video_with_controls' width=\"%0.0f\" height=\"%0.0f\" controls>\
<source src='%#' type='video/MOV'/></video><ul>\
</body></html>", self.mainScrollView.frame.size.width, self.mainScrollView.frame.size.height, [assetRepresentation url]];
[webView loadHTMLString:embedHTML baseURL:nil];
then:
UIWebView *webView = [[UIWebView alloc] initWithFrame:frame];
NSString* embedHTML = [NSString stringWithFormat:#"<video controls> <source src=\"%#\"> </video>", [assetRepresentation url];
[webView loadHTMLString:embedHTML baseURL:nil];
Also I tried to store it in NSData first and then load it from there, but that causes memory problems and wont work.
Any help is highly appreciated!
Edit: As I think UIWebView is the thing that matches the iOS App visually at most, I want to use this and not for example the MPMoviePlayer .. if I am wrong there I would be glad to know how / what else looks like the original App
I guess the video url is local thats why its not working in UiWebView
so the solution would be this:
in delegate method webviewdidstartload,set up MPmovieplayer controller to play inside a UiView and yes it will look like original app as you will be playing it in a view
something like this:
Add MPMoviePlayerController's view to a UIView
When you movie file is located locale you must set the baseURL to your MainBundle and put the full path in the "src"-tag.

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

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]

starting embedded youtube video from a specific time

NSString *linkVideo = #"http://www.youtube.com/watch?v=XabyZDSzMJ0&t=0m38s";
NSString *htmlString = [NSString stringWithFormat:#"<iframe width=\"420\" height=\"315\" src=\"http://www.youtube.com/embed/XabyZDSzMJ0\" frameborder=\"0\" allowfullscreen></iframe>", linkVideo];
// Load the html into the webview
[self.myWebView loadHTMLString:htmlString baseURL:nil];
Hi, I've embedded a youTube video in my iPhone app.
How can I force starting this video from sec 38th?
thanks a lot!
Im not a expert and probly wrong but i think &t=0m38s" is for embeded video links, you might have to use &start=38".
"start=" only takes seconds also
Im probly wrong but worth a try
NSString *linkVideo = #"http://www.youtube.com/watch?v=XabyZDSzMJ0&start=38";
This starts the youtube video at the 38th second.
I think this ight be useful:
http://www.mattcutts.com/blog/link-to-youtube-minute-second/
Thnx,

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.

Resources