Playing a youtube video in iOS sdk can be done in two ways:
1) Invoke the native youtube player in the device.
2) Open the youtube video link in a WebView.
3) Autoplay the youtube video using WebView .
Method 1:
This method utilizes the Apple's URLScheme to play youtube video by launching the youtube application on the device. This involves a simple call to the openURL method of UIApplication class as below:
NSURL *url=[NSURL URLWithString:@"http://www.youtube.com/v/IkvXcvb7rkg?f=user_favorites&app=youtube_gdata\"];
[[UIApplication sharedApplication] openURL:url];
This method may seem easy, but it's at the cost that the video will be played in other application
and your application will not be called even after the video playing is finished.
There is no other way to return to your application after video playing is done.
Method 2:
This method involved a little bit of tweak around the UIWebView control.
In this method a youtube embed code is constructed and is loaded in the WebView control in the application.
The WebView displays the video thumbnail and a play button over the thumbnail. As the user taps on the play
button the video is launched in QuickTime player and after the video playing is done, control is returned to the application.
(YouTube video in WebView with play button )
So, with a little bit of tweak we can retain the control in the application.
NSString *htmlString =@"<html><head>"
"<meta name = \"viewport\" content = \"initial-scale = 1.0, user-scalable = no, width = 212\"/></head>"
"<body style=\"background:#FFFFF;margin-top:20px;margin-left:0px\">"
"<div><object width=\"320\" height=\"240\">"
"<param name=\"wmode\" value=\"transparent\"></param>"
"<embed src=\"http://www.youtube.com/v/Hu684V2lB3Q?f=user_favorites&app=youtube_gdata\""
"type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"320\" height=\"240\"></embed>"
"</object></div></body></html>";
[webView loadHTMLString:htmlString baseURL:nil];
Method 3:
This method takes takes the 'Method 2' one step ahead by utilizing the delegate methods of WebView control
to auto play the video in QuickTime player as soon as the WebView loading is finished.
(Playing video in QuickTime player)
You need to assign the class as WebView delegate:
webView.delegate=self;
The idea is to find the play button on the WebView and send the button touch event to the control to play the video.
Complete code will look like this:
NSString *htmlString =@"<html><head>"
"<meta name = \"viewport\" content = \"initial-scale = 1.0, user-scalable = no, width = 212\"/></head>"
"<body style=\"background:#FFFFF;margin-top:20px;margin-left:0px\">"
"<div><object width=\"320\" height=\"240\">"
"<param name=\"wmode\" value=\"transparent\"></param>"
"<embed src=\"http://www.youtube.com/v/Hu684V2lB3Q?f=user_favorites&app=youtube_gdata\""
"type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"320\" height=\"240\"></embed>"
"</object></div></body></html>";
webView.delegate=self;
[webView loadHTMLString:htmlString baseURL:nil];
- (void)webViewDidFinishLoad:(UIWebView *)_webView {
UIButton *b = [self findButtonInView:_webView];
[b sendActionsForControlEvents:UIControlEventTouchUpInside];
}
- (UIButton *)findButtonInView:(UIView *)view {
UIButton *button = nil;
if ([view isMemberOfClass:[UIButton class]]) {
return (UIButton *)view;
}
if (view.subviews && [view.subviews count] > 0) {
for (UIView *subview in view.subviews) {
button = [self findButtonInView:subview];
if (button) return button;
}
}
return button;
}
I prefer Method 3 over all the methods but you choose the one that suits your need.
Happy coding...