iOS5, uiwebview, and video - iphone

Does anyone know why the following, that worked so well all the way to 4.3, now doesn't work with the iOS5 SDK (and is there a way to get it to work)? I have a general purpose video player that uses this scheme to play multiple formats and Youtube, and would like to keep using it. It just doesn't load the video (even though the webview finishes loading). In iOS5 it does still load and play Youtube videos, just no HTTP Live Streams or non-Youtube videos (i.e., mp4's).
It works fine if I build with iOS4.3 and run it on an iOS5 device (iPad). It fails when I build with the iOS5 SDK. NOTE: I kept XCODE 3.x and iOS 4.3 around just in case.
On another note, I can get the HTML5 video tag to play the live stream and mp4's, but cannot get it to resize, no matter how many widths and heights I use.
Anyway, here is the code (UIWebview):
static NSString* kEmbedHTML = #"<html><head><meta name=\"viewport\" content=\"initial-scale=1.0, user-scalable=no, width=%0.0f, height=%0.0f\"/></head><body style=\"background:#fff;margin-top:0px;margin-left:0px\"><div><object width=\"%0.0f\" height=\"%0.0f\"><param name=\"movie\" value=\"%#\"></param><param name=\"wmode\"value=\"transparent\"></param><embed id=\"yt\" airplay=\"allow\" src=\"%#\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"%0.0f\" height=\"%0.0f\"></embed></object></div></body></html>";
....
- (void)layoutSubviews {
[self stringByEvaluatingJavaScriptFromString:
[NSString stringWithFormat:#"controls.width = %0.0f; controls.height = %0.0f", kDefaultWidth,kDefaultHeight]];
}
....
NSString* html = [NSString stringWithFormat:kEmbedHTML, kDefaultWidth, kDefaultHeight, kDefaultWidth, kDefaultHeight, _urlPath, _urlPath, kDefaultWidth, kDefaultHeight];
[self loadHTMLString:html baseURL:nil] ;

try replacing "embed" with "iframe" in your html
if it works for your non-youtube stuff, you can make your youtube links work with this by replacing "watch?v=" with "embed/" in the link code

The only solution I could find was to sniff the type of URL (i.e, Youtube) and use loadRequest for the non-Youtube stuff, and loadHTMLString for the Youtube stuff.
Pretty hacky, but it works for me and gets me past this iOS5 grief.

Related

play youtube video in iphone app in ios version below 5.1.1?

I want to play a youTube video using youtube video url.
I succeed in playing a video in iOS version 5.1.1 but the video does not played in iOS version 5.0.1 and 4.0 or below. I am using webview to play video.
for IOS below 5 you need to use iframe :
NSString *youTubeVideoHTML = #"<iframe id=\"yt\" class=\"youtube-player\" type=\"text/html\" width=\"280\" height=\"186\" src=\"http://www.youtube.com/embed/xxxxxx\" frameborder=\"0\">";
and for ios 5 above we can use embed :
NSString *youTubeVideoHTML = #"<html><head>\
<body style=\"margin:0\">\
<embed id=\"yt\" src=\"http://www.youtube.com/watch?v=xxxxxx?autoplay=1\" type=\"application/x-shockwave-flash\" \
width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";
Note: In IOS 6 i found out that url works only if we pass youtube url as : http://www.youtube.com/v/xxxxxx?autoplay=1
Hope this works out.
From your comment it looks like you are using the wrong embed code, youtube embeds now look like
<iframe width="560" height="315" src="http://www.youtube.com/embed/Cw2RzvK13F4" frameborder="0" allowfullscreen></iframe>
As mentioned before flash won't play on the iPhone, so it needs to use the HTML5 version, unfortunately I don't think 100% of youtube videos have been converted, but most should work.
You need to make sure your using the proper URL format for embedded videos. They changed it somewhat recently.
Youtube Embedded Players and Player Parameters - YouTube - Google Developers
It looks like this:
http://www.youtube.com/embed/VIDEO_ID
Here is my code which works well in all previous version of iOS 5.Just give a try with this code. I am sure this is going to fix your problem.
// Over here Just replace this url with your one's.
NSString *pdfString = #"http://www.youtube.com/watch?v=qe39vPFabuA";
NSString *htmlString = #"<html><head>\n"
"<meta name = \"viewport\" content = \"initial-scale = 1.0, user-scalable = no, width = 320\"/></head>\n"
"<body style=\"background:FFF;margin-top:0px;margin-left:0px\">\n"
"<div><object width=\"320\" height=\"416\">\n"
"<param name=\"movie\" value=\"%#\"></param>\n"
"<param name=\"wmode\" value=\"transparent\"></param>\n"
"<embed src=\"%#\"\n"
"type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"320\" height=\"416\"></embed>\n"
"</object></div></body></html>\n";
[webView loadHTMLString:[NSString stringWithFormat:htmlString,[pdfString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]],[pdfString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]] baseURL:nil];
Also, make sure to test the code in Device only. Testing on Simulator won't play the Video.

Load YouTube outside Application

I have a simple UIWebView showing a page including YouTube thumbnail. When I click it, it loads it inside the application, but I want it to be loaded outside application in the YouTube-app.
How can this be done?
Thanks.
It should be as simple as loading the YouTube-video-url like this:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.youtube.com/watch?v=abcde12345"]];
It won't work in the simulator because it doesn't have the YouTube-app, but it will work on the iPhone (i hope, untested).
(I also see that your accept-rate is pretty low, you should click the little ✔ next to the answer that you think is the best answer. That way people wondering about the same thing can find the most useful answer fast and the person posting the answer will be accredited.)
EDIT:
Here is an example of what you could do to catch the YouTube-urls and opening them in the YouTube-app instead of in your app:
- (BOOL)webView:(UIWebView *)wv shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
// Determine if we want the system to handle it.
NSURL *url = request.URL;
if ([url.host isEqual:#"youtube.com"] && ([url.query rangeOfString:#"watch"] != 0)) {
if ([[UIApplication sharedApplication]canOpenURL:url]) {
[[UIApplication sharedApplication]openURL:url];
return NO;
}
}
return YES;
}
This is not tested, but should work :)
From http://apiblog.youtube.com/2009/02/youtube-apis-iphone-cool-mobile-apps.html
Method 2: Embed the YouTube player in a UIWebView
No, the iPhone still doesn't do Flash, but if you haven't already
noticed, the Safari browser on the iPhone is clever enough to turn any
YouTube embed into a clickable thumbnail that launches the native
YouTube player app on the phone. You can take advantage of this
feature in your app by using a UIWebView. Here's how:
Set up a UIWebView in your app. You can make it part of a xib or create it programmatically. Size the UIWebView according to how large
you want the clickable thumbnail to be.
Grab the video url using the same method as the one described above.
Call the loadHTMLString:baseURL: method on the UIWebView instance with some carefully constructed HTML that contains the YouTube
embedded player code snippet and some supporting HTML to make sure
that the video thumbnail appears correctly. Set the base URL to the
URL of your website (it doesn't do anything here -- ordinarily
UIWebView uses it to handle relative URL links correctly).
The best way to illustrate this is with a code snippet. Note the use
of the viewport HTML meta parameter and the consistent use of width
and height parameters throughout.
// webView is a UIWebView, either initialized programmatically or
loaded as part of a xib.
NSString *htmlString = #" ";
[webView loadHTMLString:htmlString baseURL:[NSURL
URLWithString:#"http://www.your-url.com"]];
One of the biggest benefits of this approach is that your app does not
have to quit in order for the video to start playing. In fact, the
iPhone will keep your app running in the background while it fires up
the YouTube player to play the video. After the video finishes playing
(or when the user hits "Done"), the user is automatically taken back
to your app. This experience is very similar to watching embedded
YouTube videos in the iPhone Safari browser and is just as seamless.

UIWebView not playing YouTube videos

Running into an issue where I am trying to play a YouTube video through a custom UIViewController which contains a UIWebView. All web pages load fine in the UIWebView but if I go to play a YouTube video nothing is displayed. I can hear the video and the status bar is displayed at the top the screen but I can't see any video.
I've researched this a bit to find that...
-YouTube videos may not play in the simulator so I tried it on my device (iPhone 4 with iOS 4.1) to no avail
-There may be issues when trying to play a YouTube video when tethered and deploying through XCode so I tried deploying to my iPhone and then untethering. Video still does not play and I get the same results as noted above.
I'm suspicious that a MPMoviePlayer may be getting childed to the UIWebView but the view portion of that movie player is not being set for some reason which is why I'm not seeing it. Not completely sure about that.
I am seeing alot of threads on this issue but nothing has led me to a solution yet. If there is any input you can offer it would be greatly appreciated.
I Assume you're using Embed Video, i have created HTML file and inserted the Embed video tags and in my web view i use the following:
[WebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"AboutiPad" ofType:#"html"] isDirectory:NO]]];
it will work for both Simulator and devices this snippet is tested and is being used now, Good luck
Are you trying to play a youtube video with an https URL?
I've noticed that if I try to embed videos with https, they don't always work on some devices.
I basically used the info from this post: Embedding YouTube videos on
But made sure I ran my URL parameter through the following code before putting it into the HTML template:
NSRange range = [urlString rangeOfString:#"https"];
if (range.length == 5 && range.location == 0) {
urlString = [urlString stringByReplacingCharactersInRange:range withString:#"http"];
}

Is it possible to play vimeo video in iphone native application using UIWebView approach?

There is UIWebView approach to play YouTube video inside native IPhone Application.
Does anybody tried to do the sane for vimeo?
It's only possible if Vimeo supports HTML5 video, which looks like it does.
NSString *htmlString = [NSString stringWithFormat:
#"<html>"
#"<body>"
#"<meta name = \"viewport\"content = \"initial-scale = 1.0, user-scalable = no\"/>"
#"<iframe src=\"http://player.vimeo.com/video/8118831title=0&byline=0&portrait=0&color=008efe&amp\";autoplay=1&loop=1 width=\"320\" height=\"480\" frameborder=\"0\">"
#"</iframe>"
#"<body style=\"background:#000;margin-top:0px;margin-left:0px\">"
#"</object></div></body></html>",#"http://www.vimeo.com/8118831"
];
Now just use loadHTMLString to play the video in your application.
It is only possible when user has shared his video using "plus" account. Otherwise vimeo will render static image in Iphone.

Documents/Images display fine in Simulator, but not on my device

My application is running fine on the simulator, but not on the device. Basically, any documents from the web (PDF, Word, M4P videos) are working great on the simulator (and can be accessed from Safari on the iPhone). However, running on the iPhone, they don't display anything. Here's some sample code:
// Set up the URL
documentViewController.documentUrl = [NSURL URLWithString:[NSString stringWithFormat:mobileContentUrl]];
// mobileContentUrl is something like: http://www.myserver.com/pathitem/Video.mp4" and can be accessed from Safari
[self.navigationController pushViewController:documentViewController animated:YES];
Within documentViewController I have:
- (void)viewDidLoad {
[super viewDidLoad];
[webView loadRequest:[NSURLRequest requestWithURL:documentUrl]];
}
As I said, works like a charm in the Simulator...What could possibly causing this NOT to work on iPhone? How do I debug the issue???
The most likely cause is that you're trying to load a format that isn't supported by the phone. The Simulator uses many libraries from the Mac, so it often has much greater support than is available on the phone.
Can you access these URLs using MobileSafari on the phone?
PDF should work on the iPhone, as long as it doesn't require too much memory to display. The Simulator has pretty much unlimited memory while the iPhone is limited by the hardware, which I'm not sure but I think you have about 32 MB to play with on the oldest iPhones.
I'm not sure about the other formats. When you wrote M4P, did you mean mp4, or do you really mean M4P, because that is a protected AAC file that the iTunes Music Store uses, and you can't play those except through certain methods that enforce copy protection.