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"];
}
Related
NSURL *youTubeURL = [NSURL URLWithString:#"http://www.youtube.com/watch?v=HPwyVw_6vsQ&feature=youtu.be"]; // URL is a string which needs to be placed in #"" and close method call with ].
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:youTubeURL];
imageSel = [player thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame];
I am using this code for getting thumbnail from the video and this return null value in the
imagesel.can anybody help me to solve this problem I am using YouTube URL .when I am using this code for the video that is in the main bundle then it is working fine.
Do this:
Firstly extract videoID from Video URL Link.
Now refer this link for getting thumbnail of video.
each youtube link has 4 thumbnail
http://img.youtube.com/vi/<insert-youtube-video-id-here>/0.jpg
http://img.youtube.com/vi/<insert-youtube-video-id-here>/1.jpg
http://img.youtube.com/vi/<insert-youtube-video-id-here>/2.jpg
http://img.youtube.com/vi/<insert-youtube-video-id-here>/3.jpg
this code is very good for
for example
i have this link:
http://www.youtube.com/watch?v=HPwyVw_6vsQ&feature=youtu.be
and the youtube video id is:
HPwyVw_6vsQ
and generated thumbnail from this link is:
http://img.youtube.com/vi/HPwyVw_6vsQ/1.jpg
this is very easy to use
if someone wants HQ image use this link
https://img.youtube.com/vi/<insert-youtube-video-id-here>/maxresdefault.jpg
If you have a YouTube watch page URL, and you want to get the thumbnail URL, the "correct" way to do that is to use oEmbed:
http://apiblog.youtube.com/2009/10/oembed-support.html
That way you don't have to attempt to parse the video id out of the URL.
But... what you really should be doing is using a iframe embed to display videos in your web page (or UIWebView). The YouTube API Terms of Service covers the supported ways of accessing YouTube content:
https://developers.google.com/youtube/terms
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.
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.
I am trying to show YouTube videos on my application. How can I stop/pause the playing movie player after a particular amount of time. I am using/loding the url in webview rather than MPMoviePlayer. If anyone knows the solution please share. I need to implement it in one of my app.
Thanks in advance.
As you will not get any access to the actual MPMoviePlayerController and since no notifications will be visible outside the UIWebView, you will be locked into using javascript code - that is, stay within the UIWebView scope of actions.
How and if such things are exactly implemented/able using javascript, I do not know.
I'm having a few issues playing a video in MonoTouch. From what I can find there are two different approaches to take. Both result in the audio being played but no video. I'm betting I'm missing something simple so any help would be great.
Attempt one - taken from MT documentation
moviePlayer = new MPMoviePlayerController(new NSUrl("test.mp4"));
moviePlayer.Play();
Attempt two
moviePlayer = new MPMoviePlayerViewController(new NSUrl("test.mp4"));
this.PresentMoviePlayerViewController(moviePlayer);
Thanks
From looking at the documentation, you need to add the MPMoviePlayerController to a view, otherwise the video will no know where to play.
Your second attempt looks a little better, are you calling the play method on the moviePlayer (note this is a MPMoviePlayerViewController) MPMoviePlayerController (named MoviePlayer)?
For what it's worth, I can get;
moviePlayerController = new MPMoviePlayerViewController(new NSUrl("test.mp4"));
this.PresentMoviePlayerViewController(moviePlayerController);
working with no issues on a sample iPad app. Are you sure your test.mp4 is...
Encoded correctly
Included in the project
Video's build action is set to content
Documentation for MPMoviePlayerController: http://developer.apple.com/library/ios/#documentation/MediaPlayer/Reference/MPMoviePlayerController_Class/MPMoviePlayerController/MPMoviePlayerController.html
Documentation for MPMoviewPlayerViewController:
http://developer.apple.com/library/ios/#documentation/MediaPlayer/Reference/MPMoviePlayerViewController_class/Reference/Reference.html#//apple_ref/occ/cl/MPMoviePlayerViewController