I Want to show the the youtube video in an UIWebView and I am able to show the Video in an UIWebView with the help of following function to embed the YouTube URL.
- (void)embedYouTube:(NSString*)url
{
NSString *embedHTML = #"<html><head><meta name = \"viewport\" content = \"initial-scale = 1.0, user-scalable = no, width =\"%0.0f\"/></head><body style=\"background:transparent;margin-top:0px;margin-left:0px\"><div><object width=\"212\" height=\"172\"><param name=\"movie\" value=\"%#\"></param><param name=\"wmode\" value=\"transparent\"></param><embed src=\"%#\"type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"%0.0f\" height=\"%0.0f\"></embed></object></div></body></html>";
//NSString* html = [NSString stringWithFormat:embedHTML, url, self.videoView.frame.size.width, self.videoView.frame.size.height];
NSString *html=[NSString stringWithFormat:embedHTML,self.videoView.frame.size.width,url,url,self.videoView.frame.size.width,self.videoView.frame.size.height];
[self.videoView loadHTMLString:html baseURL:nil];
}
Here url is the youtube url,videoView is the webview.
Now this function works just good for the normal urls like http://www.youtube.com.... but for the mobile youtube urls like http://m.youtube.com.... ,it doesn't show the video, though the video can be played on the browser.
It is working if I replace the 'm' with 'www' from the url,but I dont think so it is good idea.
Related
I want to play video in my iPhone.
NSString *myHTML = [NSString stringWithFormat:#"\<html>\<head>\<body>\%#</body>\</html>", summary];//here summary is my url
[webView2 loadHTMLString:myHTML baseURL:nil];
if I give the url statically its working fine iam able to play the video,but my url is coming from server like this
<iframe width="560" height="314' src="http://www.youtube.com" frameborder="0" allowfullscreen></iframe> // iam getting this from service.
if I proceed in the same way to load video its not working it's just showing total string in my view.
how to rectify this issue shall I need to get only source from that string(url)or is there any other way?
here i may get youtube video or mp4 or mov but not flash.(ios doesn't suppor)
It looks like your Service Response is in XML Format. First of all you need to do is to perform XML Parsing. For XML Parsing there are several Tutorials available. Some of the Links are :
Super easy iOS XML parsing
Parsing XML with Objective-C
How to Parse XML Files in Xcode
After parsing, Extract the data you want and Store it in some Variables or Arrays. Then use that data to play the Video. You can check these Links :
YouTube API Blog
Playing Video on the iPhone and iPad
Be Relax. It's so simple. Just follow the above steps and you will get the Result you want.
Update :
If your string look like myString mentioned in the below example then you can use the below code to get the URL.
NSString *myString = #"<iframe width="560" height="314'' src="http://www.youtube.com" frameborder="0" allowfullscreen></iframe>";
NSRange firstRange = [myString rangeOfString:#"http://"];
NSRange secondRange = [[myString substringFromIndex:firstRange.location] rangeOfString:#"""];
NSRange finalRange = NSMakeRange(firstRange.location, secondRange.location);
NSString *subString = [myString substringWithRange:finalRange];
NSLog(#"subString :: %#",subString);
GoodLuck !!!
1. If you want to play video in MPMoviePlayerController then use bellow code...
you can play video in MPMoviePlayerController like bellow...
MPMoviePlayerController *videoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
videoPlayer.view.frame = CGRectMake(184, 200, 400, 300);
[self.view addSubview:videoPlayer.view];
[videoPlayer play];
see whole example from this link Code for Play Video in iPhone
Also see another Demo for play the video in iPhone from this link
AVPlayer
UPDATE :
2. If you want to play video in webView then use bellow code...
NSString *Str = #"<video controls> <source src=\"yourvideofile.mp4\"> </video>";
NSString *path = [[NSBundle mainBundle] pathForResource:#"yourvideofile" ofType:#"mp4"];
[Webview loadHTMLString:Str baseURL:[NSURL fileURLWithPath:path]];
i hope its useful to you...
just call add your string in this line your are good to go
[self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"Your string"]]];
I have video embedded in my application using this solution (html string in UIWebView):
http://iphoneincubator.com/blog/audio-video/how-to-play-youtube-videos-within-an-application
Unfortunately client requires his own thumbnail. I would like to solve it by creating UIWebView once he taps on this thumbnail and play the video inside automatically.
How can I do it?
Try this:-
NSString *htmlString = [NSString stringWithFormat:#"<html><head>"
"<meta name = \"viewport\" content = \"initial-scale = 1.0, user-scalable = no, width = 50\"/></head>"
"<body style=\"background:#F00;margin-top:0px;margin-left:0px\">"
"<div><object width=\"50\" height=\"50\">"
"<param name=\"movie\" value=\"%#\"></param>"
"<param name=\"wmode\" value=\"transparent\"></param>"
"<embed src=\"%#\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"50\" height=\"50\"></embed>"
"</object></div></body></html>",[d objectForKey:#"hrefUrl"],[d objectForKey:#"hrefUrl"]];
[videoView loadHTMLString:htmlString baseURL:[NSURL URLWithString:#"http://www.your-url.com"]];
Following is the html string to play video automatically inside UIWebView...
NSString *embedHTML = [NSString stringWithFormat:#"<html><body><video controls=\"controls\" autoplay=\"autoplay\"><source src=\"%#\" type=\"video/mp4\"/></video></body></html>", url];
[_webView loadHTMLString:embedHTML baseURL:baseURL];
I have tried the following one, it is working fine and also starts to play automatically.
NSString *embedHTML = #"<html><body bgcolor="black"><embed id="yt" src="http://www.youtube.com/watch?v=9vyYHVB-QnY"type="application/x-shockwave-flash" width="320.00" height="370.00"></embed></body></html>";
[_webView loadHTMLString:embedHTML baseURL:baseURL];
I am using this code to see youtube videos
- (YouTubeView *)initWithStringAsURL:(NSString *)urlString frame:(CGRect)frame;
{
if (self = [super init])
{
// Create webview with requested frame size
self = [[UIWebView alloc] initWithFrame:frame];
// HTML to embed YouTube video
NSString *youTubeVideoHTML = #"<html><head>\
<body style=\"margin:0\">\
<embed id=\"yt\" src=\"%#\" type=\"application/x-shockwave-flash\" \
width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";
// Populate HTML with the URL and requested frame size
NSString *html = [NSString stringWithFormat:youTubeVideoHTML, urlString, frame.size.width, frame.size.height];
// Load the html into the webview
[self loadHTMLString:html baseURL:nil];
}
return self;
}
Now I am not able to see the relative video on the simulator..however white webview is coming there..
so is there anything like that that I cannot see youtube videos on simulator or there is any other problem in the code?
You can't see Youtube video in simulator. If white webview is showing, I think your code works.
Try installing on your iPhone/iPod touch.
I am trying to embed a video with the following code to avoid launching the youtube app and stay in my app:
-(void)embedYouTube:(NSString*)url frame:(CGRect)frame {
NSString* embedHTML = #"<iframe type=\"text/html\" width=\"64\" height=\"39\" src=\"http://www.youtube.com/embed/j8Bc7eRTdWY\" frameborder=\"0\"></iframe>";
if(videoView == nil) {
videoView = [[UIWebView alloc] initWithFrame:frame];
[movieDescView addSubview:videoView];
}
[videoView loadHTMLString:embedHTML baseURL:nil];
}
The problem is when I clicked on the play icon, it opens the youtube website with my uiwebview asking to install some web app . I would like to prevent this web app pop up to show or start the video full screen. Actually, any solution to show a youtube video without exiting my app would do.
You can use this to load the movie (no needed to generate a html string):
movieView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 140, 100)];
movieView.delegate = self;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.youtube.com/embed/-h4zTEwgCpQ"]];
[movieView loadRequest:request];
[self.view addSubview:movieView];
#pragma mark -
#pragma mark UIWebViewDelegate
//To check if youtube try to show the overview page of m.youtube.com or the current movie
//if the user click on the youtube logo this method stop loading the mobile page
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *urlRequest = [request URL];
if ([[urlRequest absoluteString] isEqualToString:url]) {
return YES;
}
return NO;
}
The onely thing you have to looking for, is to use the "embed" link.
UIWebView *wbView = (UIWebView *)[self.view viewWithTag:1];
NSString *embedHTML = #"\
<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=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";
NSString *html = [NSString stringWithFormat:embedHTML,url, 64.0, 64.0];
[wbView loadHTMLString:html baseURL:nil];
"url" is your video url
You don't use embed if you are trying to play the video. Just use the normal video URL so it will open in the iPhone's YouTube application rather than the web. If it opens in the web it will ask to install it's web app.
You need to change your HTML to this:
Play Video
Or to launch it directly to the native app use the URL:
http://www.youtube.com/watch?v=j8Bc7eRTdWY
More Reference Information:
Apple Reference on URL Schemes
Apple Reference on Youtube Links
Tips:
stringUrl = [stringUrl stringByReplacingOccurrencesOfString:#"/watch?v=" withString:#"/embed/"];
where stringUrl is a video link of youtube ( ex. Youtube )
I spent quite a bit of time looking through libraries and old code snippets; this library worked for me:
https://github.com/larcus94/LBYouTubeView
I am trying to play a youtube video in a UIWebView instead of leaving my application.
Google thinks is easy peasy- http://apiblog.youtube.com/2009/02/youtube-apis-iphone-cool-mobile-apps.html
So I have the GData framework and headers working nicely, and I have no problem doing queries, loading user's video feeds etc.
But what I can't seem to do is load a specific video's feed. I know the ids of the videos that I want the feeds for in advance. How do i load a specific video's feed?
I'm then going to follow google's instruction :
Grab the video url from the media tag in the API response with the application/x-shockwave-flash type.
and then embed it like so:
// webView is a UIWebView, either initialized programmatically or loaded as part of a xib.
NSString *htmlString = #"<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=\"212\" height=\"172\">
<param name=\"movie\" value=\"http://www.youtube.com/v/oHg5SJYRHA0&f=gdata_videos&c=ytapi-my-clientID&d=nGF83uyVrg8eD4rfEkk22mDOl3qUImVMV6ramM\"></param>
<param name=\"wmode\" value=\"transparent\"></param>
<embed src=\"http://www.youtube.com/v/oHg5SJYRHA0&f=gdata_videos&c=ytapi-my-clientID&d=nGF83uyVrg8eD4rfEkk22mDOl3qUImVMV6ramM\"
type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"212\" height=\"172\"></embed>
</object></div></body></html>";
[webView loadHTMLString:htmlString baseURL:[NSURL URLWithString:#"http://www.your-url.com"]];
Any help would be much appreciated!
Given a feed of YouTube video entries, you can get the IDs and Flash URLs from each entry this way:
for (GDataEntryYouTubeVideo *videoEntry in [feed entries]) {
GDataYouTubeMediaGroup *mediaGroup = [videoEntry mediaGroup];
NSString *videoID = [mediaGroup videoID];
NSArray *mediaContents = [mediaGroup mediaContents];
GDataMediaContent *flashContent =
[GDataUtilities firstObjectFromArray:mediaContents
withValue:#"application/x-shockwave-flash"
forKeyPath:#"type"];
NSLog(#"video ID = %#, flash content URL = %#",
videoID, [flashContent URLString]);
}
In exactly the same situation just found the answer. Typically of many large APIs, common usage instructions get lost in explanation of the higher level stuff - can be frustrating. Thankfully, the info is there...
http://code.google.com/apis/youtube/2.0/developers_guide_protocol_video_entries.html
So, we should be able to plug the returned ATOM feed into the GData library and have it parse out the proper content URL for use in the 'UIWebView' style player code.
...Grobbins, read the question properly next time!