So it’s very easy to play a video in your iPhone app — whether local or off the web — with MPMoviePlayerController; and it’s very easy to put a video on the web via YouTube. However, there is a problem combining the two; since YouTube doesn’t give out direct links to its videos, you can’t give a YouTube video to MPMoviePlayerController. And if you tell the system to open the URL, well then your app’s gone. A non-optimal experience, indeed.
But here’s a way around that — just have a UIWebView containing nothing but the EMBED code for the link you want to pull up, and it’ll show up — once it loads, so you do have a white flash while you wait — as a YouTube brand, play button, and frame from the video. When the user taps that, the YouTube application is launched on top of your app, and when it’s finished, control is returned. Not bad on the convenience, indeed.
Unfortunately, there’s no kind of direct control or notifications of loading, progress, quitting, etc. However, you can get some indirect notifications based on your application’s window state: add in your view controller
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(windowNowVisible:)
name:UIWindowDidBecomeVisibleNotification
object:self.view.window
];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(windowNowHidden:)
name:UIWindowDidBecomeHiddenNotification
object:self.view.window
];
to get these called when the YouTube window is shown and goes away respectively.
- (void)windowNowVisible:(NSNotification *)note
{
twlog("YouTube window went away");
}- (void)windowNowHidden:(NSNotification *)note{twlog("YouTube window took over");}
and hey, if that’s all you require by way of notification you’re good!
Now, if some clever spark out there would tell me how to set up the HTML delineated in that article so it would actually display an image of my choice instead of what comes from YouTube, then I’d be really happy! Bueller? Bueller? Anyone?
Continue Reading →AUG