For playing youtube videos, I'm using
-(void)playYoutubeVideo:(NSString *)youtubeUrl {
NSString *videoId = [self extractYoutubeIdFromLink:youtubeUrl];
NSLog(@"video Id %@",videoId);
[self playVideoWithId:videoId];
}
- (NSString *)extractYoutubeIdFromLink:(NSString *)link {
NSString *regexString = @"((?<=(v|V)/)|(?<=be/)|(?<=(\\?|\\&)v=)|(?<=embed/))([\\w-]++)";
NSRegularExpression *regExp = [NSRegularExpression regularExpressionWithPattern:regexString
options:NSRegularExpressionCaseInsensitive
error:nil];
NSArray *array = [regExp matchesInString:link options:0 range:NSMakeRange(0,link.length)];
if (array.count > 0) {
NSTextCheckingResult *result = array.firstObject;
return [link substringWithRange:result.range];
}
return nil;
}
- (void)playVideoWithId:(NSString *)videoId {
NSString *youTubeVideoHTML = @"<html><head><style>body{margin:0px 0px 0px 0px;}</style></head> <body> <div id=\"player\"></div> <script> var tag = document.createElement('script'); tag.src = 'http://www.youtube.com/player_api'; var firstScriptTag = document.getElementsByTagName('script')[0]; firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); var player; function onYouTubePlayerAPIReady() { player = new YT.Player('player', { width:'%f', height:'%f', videoId:'%@', events: { 'onReady': onPlayerReady } }); } function onPlayerReady(event) { event.target.playVideo(); } </script> </body> </html>";
NSString *html = [NSString stringWithFormat:youTubeVideoHTML,300,200, videoId];
[webView loadHTMLString:html baseURL:[[NSBundle mainBundle] resourceURL]];
}
I'm getting youtube video ID from the watch url.
Its working fine for the urls like https://www.youtube.com/watch?v=p9mddDM43wE
But, the below youtube video is not playing in UIWebView
https://www.youtube.com/watch?v=CT-fPP1fjDA
Showing the below error,
What might be problem?
Can anyone please clarify?