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.
Related
iv created an app but id like to add a feature where when i press a button it opens my spotify app and automatically plays music... if this is even possible that is... iv got most of it done i think, iv got the button and it opens spotify but cant find out how to auto play, has anyone done this or knows how to?
-(IBAction)spotify:(id)sender {
NSString *stringURL = #"spotify:";
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];
}
please help :)
You can open most spotify: URLs (using [[UIApplication sharedApplication] openURL:];) and the Spotify iOS client will open and show them.
There's some documentation here - make sure you use spotify: URIs and not HTTP URLs.
However, the iOS client currently doesn't support autoplay that I'm aware of - it'll be up to the user to push play.
Alternatively, you could use CocoaLibSpotify to embed Spotify playback right inside your app.
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 have a UIWebview within a UIScrollView. Within the webview I have an embedded YouTube movie. When I play the YouTube movie everything works fine, however, when I press the fullscreen button the movie starts playing behind the webview. Bringing the WebView to the front does not work, because I want the scrolling capabilities of the ScrollView to do horizontal scrolling.
Basically, you want to be able to detect when a movie starts playing fullscreen and arrange your views appropriately. Then, when the fullscreen playback ends you want to be able to get your views back to how they are.
The problem is there's no documented way to hook into or detect when a UIWebView triggers video playback in full screen.
I said 'documented way', because there are some notifications you can use to detect when a UIWebView triggers fullscreen playback. You can just listen out for them and rearrange your views accordingly. However, I don't necessarily recommend this, because they're undocumented and subject to change (in fact, they have a spelling mistake in them up until iOS 4.3, see answers below).
// For iOS 4.3 and above:
UIMoviePlayerControllerDidEnterFullscreenNotification
UIMoviePlayerControllerDidExitFullscreenNotification
// For iOS 4.2 and below:
UIMoviePlayerControllerDidEnterFullcreenNotification
UIMoviePlayerControllerDidExitFullcreenNotification // (note spelling mistake)
Finding these out was a bit of a pain - there are actually several more notifications that get triggered when a YouTube video in a UIWebView gets played back. To find them out you'll need to drop a breakpoint on all posting of notifications, and then manually inspect the memory locations of those notifications to figure out their string names. I don't actually think anyone has pulled these out before, because when I google them I get nothing. But they do work, promise!
I hope being able to get notified when the fullscreen playback is entered/exited will be helpful, with the massive caveat that Apple could change this undocumented behavior at any time. Your app won't get rejected outright for using them (because you're not calling any undocumented methods), and I've used it in shipping apps. But it's still not the best idea in the world...may be the only option you have though.
Edit: To clarify, based off the comment below: they're not constants, so you'd need to put them in quotes if you were registered for notifications.
For iOS 4.3+, Apple have changed the names of these notifications:
UIMoviePlayerControllerDidEnterFullcreenNotification now is UIMoviePlayerControllerDidEnterFullscreenNotification
UIMoviePlayerControllerDidExitFullcreenNotification now is UIMoviePlayerControllerDidExitFullscreenNotification
Please pay attention: The term "Fullcreen" has changed to "Fullscreen".
Thanks!
Just wanted to confirm that this works after playing around with it for awhile. You can get direct access to the view that the video is playing in. This is a blocks method of registering for the notification and pulling out the pertinent views.
Notice: You will need to delay adding subviews and/or access Apple internal subviews to remove the navbar for further customization.
[[NSNotificationCenter defaultCenter]
addObserverForName:#"UIMoviePlayerControllerDidEnterFullcreenNotification"
object:nil
queue:nil
usingBlock:^(NSNotification *note){
MPMoviePlayerController *theMovieController = [note object];
UIView *theDestinationVideoView = [[note userInfo]
objectForKey:#"UIMoviePlayerControllerFullscreenViewUserInfoKey"];
}];
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"];
}