iPhone YouTubePlayer
I’ve been plodding away on a project (it’s getting there almost) and one of the things that I really wanted was to provide video tutorials for the user. I really don’t want to package all these videos with the application, really once you’ve watched them once you will probably never look at them again (If I’m wrong about that let me know ).
The new idea is to stream them down to the device, once option was to use the MoviePlayer provided with the sdk. This is fine if you want to host your own video content, but I’m not that confident that I would be able to provide a reliable content service. Whats the alternative then, well there is always YouTube and the iPhone has a compatible way of playing back videos even inside the application. Unfortunately to play these YouTube videos you need to use a UIWebView where the simplest version of a player would present the user with a link to play the video. Not the most elegant solution but it works, but I was not really happy with the solution.
I decided I could do better, I really wanted a player that provided complete application integration and the resulting Player is just that. The player is easy to use and has simplest API I could come up with, it’s also very easy to integrate with any application. Go on and try it out.
//
// YouTubePlayer.h
//
// Created by Robert on 18/12/2009.
//
#import <Foundation/Foundation.h>
@interface YouTubePlayer : NSObject <UIWebViewDelegate, UIAlertViewDelegate>
{
@private
UIView * background;
UIWebView * webView;
UIActivityIndicatorView * activityIndicator;
}
- (void) playbackVideo:(NSString*)_videoID InView:(UIView*)_view;
@end
Example use:
// Inside a UIViewController
- (void) play
{
// create the video player object
if ( videoPlayer == nil )
{
videoPlayer = [[YouTubePlayer alloc] init];
}
// Request the video to be played using the video id
[videoPlayer playbackVideo:@"MuU00Q3RhDg" InView:self.view];
}
As always the source is provided as is and use at your own risk, also feel free to alter and use as you want I don’t mind. Have Fun.
Download : YouTubePlayer.h, YouTubePlayer.m
Note: You will need to include the SystemConfiguration framework as this is used to check for the availability of a web connection.
Thanks to the following resources that make it possible to build this player:



Hello from Greece,
I would like to thank you for your great code tutorials.
I am now learning objective c and this code is very helpful.
However i have trouble putting it all together.
in the example use block above you say to put the code inside a UIViewController.
I think i have done that but nothing appears when i run it in the device.
I would be gratefull if you could email me a sample project so i can see
how everything is connected. (mythodeia@gmail.com)
Thank you very much
@Konstantinos
Hi,
Sorry for the delay in getting back to you, it’s been a while since I’ve looked at this code and not sure that it will still work.
I’ll try and put together a code sample tomorrow to test it out, I’ll also upload it once I have it all working.
Cheers,
Bert