Reputation: 1580
I want to play youtube videos in my iOS App,I saw that there are many options like embedding video and all in UIWebview
.But i want it like a player.When i click the button then it opens new page and the video should play .
I even searched in google then i heard about this control
"XCDYouTubeVideoPlayerViewController-master" When i integrate this to my project
like
inside my youtube.m
file
in viewdidAppear()
i have added this code
XCDYouTubeVideoPlayerViewController *videoPlayerViewController = [[XCDYouTubeVideoPlayerViewController alloc] initWithVideoIdentifier:@"9bZkp7q19f0"];
[self presentMoviePlayerViewControllerAnimated:videoPlayerViewController];
and then it opening a video player and within seconds the video player closing .Its not playing anything.
When i tried to embed video
webView.frame=CGRectMake(0,0,self.view.frame.size.width, self.view.frame.size.height);
NSLog(@"%@",sharedManager.trailerLink);
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:sharedManager.trailerLink]]];
am getting a window like youtube mobile site please check image.i want only one video in my screen .
Please help me
Upvotes: 0
Views: 1392
Reputation: 2329
this library meets your goals and is easy to use test HCYoutubeParser, I hope it will be useful!
Upvotes: 0
Reputation: 1430
It is not possible to load a youtube video with the same controls as a MPMoviePlayer
-object, but you can skip loading the related content by using YouTubes iFrame-API
To load videos in a UIWebview, use this helper:
- (NSString*)youTubeHTMLFromURL:(NSURL*)url
{
NSError *error = NULL;
NSRegularExpression *regex =
[NSRegularExpression regularExpressionWithPattern:@"(?<=v(=|/))([-a-zA-Z0-9_]+)|(?<=youtu.be/)([-a-zA-Z0-9_]+)"
options:NSRegularExpressionCaseInsensitive
error:&error];
NSTextCheckingResult *match = [regex firstMatchInString:url
options:0
range:NSMakeRange(0, [url length])];
if (match) {
NSRange videoIDRange = [match rangeAtIndex:0];
NSString *videoId = [url substringWithRange:videoIDRange];
NSString *html = [NSString stringWithFormat:@"<!DOCTYPE html>\
<html>\
<head>\
<style type='text/css' media='screen'>\
body, p{\
margin: 0px;\
}\
#player {\
width: 100%%;\
}\
</style>\
</head>\
<body><div id='player'></div>\
<script type='text/javascript'>\
var tag = document.createElement('script');\
tag.src = 'https://www.youtube.com/iframe_api';\
var firstScriptTag = document.getElementsByTagName('script')[0];\
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);\
\
var player;\
function onYouTubeIframeAPIReady() {\
player = new YT.Player('player', {\
height: '390',\
width: '640',\
videoId: '%@',\
events: {\
'onReady': onPlayerReady,\
}\
});\
}\
\
function onPlayerReady(event) {\
event.target.playVideo();\
}\
\
</script>\
</body>\
</html>", videoId];
return html;
}
return nil;
}
Upvotes: 1