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!
Related
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.
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 want to show the soap response to UIWebview..
my soap response is,
<p><img src="http://img.gawkerassets.com/img/17u1vg3xyeuipjpg/xlarge.jpg" width="410" data-size="large"/></p><p><a rel="lytebox" href="http://img.gawkerassets.com/img/17u1vg3xyeuipjpg/original.jpg"> <p></a>Before sharing a playlist of songs with a crush was as simple as a few clicks, the mixtape was a labour of love; a time-consuming project. Every song was carefully considered. That’s all gone now, of course, but this DIY MP3 player conjured up by MakerBot can help bring it back.<img src="http://feeds.feedburner.com/r/GizmodoAustralia/4/zFs-k-3__1I" height="1" width="1"/>
like above..
am using this code for load the soap response to UIWebview
[webView loadHTMLString:soapresponse baseURL:nil];
in webview i want to clear the tags and display the contents.
anyone please help me to do this..
Thank you
EDIT Decoding HTML entities like & lt; to < : use NSString+HTML
NSString *strEncodeHTML = #"<p><img src="img.gawkerassets.com/img/17u1vg3xyeuipjpg/xlarge.jpg" width="410" data-size="large"/></p><p><a rel="lytebox" href="img.gawkerassets.com/img/17u1vg3xyeuipjpg/…; <a href="img.gawkerassets.com/img/17u1vg3xyeuipjpg/original.jpg" rel="modal"></a> <p></a>content here.img src="feeds.feedburner.com/r/GizmodoAustralia/4/zFs-k-3__1I" height="1" width="1"/>";
strEncodeHTML = [[[strEncodeHTML stringByReplacingOccurrencesOfString:#"<" withString:#"<"] stringByReplacingOccurrencesOfString:#">" withString:#">"] stringByReplacingOccurrencesOfString:#""" withString:#"\""];
NSLog(#"%#",strEncodeHTML);
NSString *strHTML = [NSString stringWithFormat:#"<html><body>%#<body></html>",strEncodeHTML] ;
[webView loadHTMLString:strHTML baseURL:nil];
Load html content like this:
NSString *strHTML = [NSString stringWithFormat:#"<html><body>%#</html>/<body>",yourSoapresponse];
[webView loadHTMLString:strHTML baseURL:nil];
Below given is example:
NSString *strHTML = [NSString stringWithFormat:#"<html><body><p><img src=\"http://img.gawkerassets.com/img/17u1vg3xyeuipjpg/xlarge.jpg\" width=\"410\" data-size=\"large\"/></p><p><a rel=\"lytebox\" href=\"http://img.gawkerassets.com/img/17u1vg3xyeuipjpg/original.jpg\"> <p></a>Before sharing a playlist of songs with a crush was as simple as a few clicks, the mixtape was a labour of love; a time-consuming project. Every song was carefully considered. That’s all gone now, of course, but this DIY MP3 player conjured up by MakerBot can help bring it back.<img src=\"http://feeds.feedburner.com/r/GizmodoAustralia/4/zFs-k-3__1I\" height=\"1\" width=\"1\"/></html>/<body>"];
[webView loadHTMLString:strHTML baseURL:nil];
I don't know how to parse HTML page content in iPhone SDK?
I have an iPhone app, in this app I need to show the image and data from the HTML page. I have an HTML page URL, I need to get data from the HTML page URL. Can anyone please guide me to parse HTML content from HTML page and show in iPhone app?
Can you please help me?
EDIT
I have an website in HTML format like this http://www.example.com/mobile/403.html page. I want to develop a native iPhone app for this website. My client wont give the response in XML feed so i need to use this site and parse the HTML contents. This page having many images, live data and tables. Till now i didn't parsed HTML page/content in iPhone SDK. So i need your help to do this? Can you please help me? I hope it is clear comparing with old question. Thanks in advance.
You can add the the image url to the NSMutableArray as code below..
NSMutableArray *array = [[NSMutableArray alloc] init];
NSString *response = [[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"index1" ofType:#"html"] encoding:NSUTF8StringEncoding error:nil];
// NSLog(#"response == %#", response);
NSString *regexStr = #"<a href=\"([^>]*)\">";
//NSString *regexStr = #"<A HREF=\"([^>]*)\">";
NSError *error;
NSInteger i =0;
// NSInteger length =0;
while (i<[response length]) {
NSRegularExpression *testRegex = [NSRegularExpression regularExpressionWithPattern:regexStr options:NSRegularExpressionCaseInsensitive error:&error];
if( testRegex == nil ) NSLog( #"Error making regex: %#", error );
NSTextCheckingResult *result = [testRegex firstMatchInString:response options:0 range:NSMakeRange(i, [response length]-i)];
// NSLog(#"result == %#",result);
NSRange range = [result rangeAtIndex:1];
if (range.location == 0) {
break;
}
NSString * imageUrl = [response substringWithRange:range];
if ([imageUrl hasSuffix:#".jpg"] || [imageUrl hasSuffix:#".gif"] || [imageUrl hasSuffix:#".tiff"] || [imageUrl hasSuffix:#".JPG"] || [imageUrl hasSuffix:#".JPEG"] || [imageUrl hasSuffix:#".png"] || [imageUrl hasSuffix:#".PNG"] || [imageUrl hasSuffix:#".GIF"] || [imageUrl hasSuffix:#".TIFF"]) {
// NSLog(#"%#",imageUrl);
// imageUrl = [imageUrl stringByReplacingOccurrencesOfString:#"/syneye_Portfolio/" withString:#""];
[array addObject:imageUrl];
//[array retain];
}
i= range.location;
//NSLog(#"%i",range.location);
i=i+range.length;
}
You should get the HTML thanks to a networking framework (AFNetworking for instance) and parse it with one of the following options:
custom code
some HTML parsing framework (i don't know any)
loading the HTML in a hidden UIWebView and doing some Javascript inside with stringByEvaluatingJavaScriptFromString:
I still don't understand exactly what you want, but this is what's possible with urls and html pages:
1) The page can be loaded into Safari. Of course a user can do this if the user has the url. But also a native app can launch Safari and supply Safari with the url of the page to load. A native app cannot however launch Safari and give it the actual html page to load, it must the url.
2) A native app can use UIWebView to either a) download and display a html page given a url or: b) If the html exists on the device, then the html page is given to UIWebView then the UIWebView will display it directly.
3) If the intention is to just extract a bit of text or an image or two from the html and then display it,then search for a few postings/tutorials then you can a) load the html page in a hidden UIWebView and use Javascript to access the dom elements or b) if its xhmlt use an xml parser to extract the html tags you are looking for or c) see if there are html parsing frameworks available for html or d) do a hack and string seach for the html tokens in the html directly.
4) Parse the full HTML and display all its contents yourself. Unless this is a very very simple html, without a full set of features and can't handle javascript etc. etc. Do you have a large team and years free in which to write what would effectively be your own browser.
EDIT:
You still keep talking about parsing as if parsing is the same thing as displaying it.
If you just want to display the http page at the url use UIWebView. The UIWebView will parse it AND display it. There's no need for you to parse it yourself. Or launch Safari from you app (but you won't be able to return to your app afterwards).
You say you can't use UIWebView? Why not?
To actually try and parse and display the HTML page yourself would be madness.
I'm currently creating a twitter client for iphone.
Basically, I have three kinds of url types I want to handle:
Standard URL's (e.g. http://www.msn.com)
YouTube URL's
URL's containing links to a stream.
When a user clicks a link.. instead of launching the web view and letting the webview decide whether to load quicktime player & youtube player, I want to be able to handle it within the app.
i.e. how can i code something like:
check link
if link = link.youtube then
load youtube player
if link = link.mp3 stream then
load quicktime player
else
load in webview
Checking youtube is easy, you can just check the domain like
[NSString rangeOfSubstring:#"youtube.com"].location != NSNotFound
The mp3 stream is not so hard, you check for the last extension by getting the last "." and then check it agains the set of video stream format like:
NSArray *possibleExtensions = [url componentsSeparatedByString:#"."];
NSString *extension = [possibleExtensions objectAtIndex:([possibleExtensions count] - 1)];
NSSet *extensionSet = [NSSet setWithObjects: #"mp4", #"mov", #"m4v", #"mpv", #"3gp", nil];
BOOL isStream = [extensionSet containsObject:extension];
If you want to check for normal link, you can use Regular expression:
NSString *regrexUrl = #"\\b((?:[\\w-]+://?|www[.])[^\\s()<>]+(?:\\([\\w\\d]+\\)|(?:[^\\p{Punct}\\s]|/)))";
// Based on http://daringfireball.net/2009/11/liberal_regex_for_matching_urls
NSString *urlString = [YOUR_STRING_HERE stringByMatching:NCPARSER_REGEX_URL capture:1];
if (urlString) {
NSLog(#"It is an url");
}