Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Some youtube videos not playing in UIWebView. Showing - (An error occurred please try again later)

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,

enter image description here

What might be problem?
Can anyone please clarify?

like image 824
Nazik Avatar asked Aug 04 '16 07:08

Nazik


Video Answer


1 Answers

Recently I faced the same issue in my iOS application where we are loading a YouTube video link inside UIWebView.

After breaking head for long time, it turned out to be the device "date and time" settings problem.

The device date was set to very old. After we updated the settings, the youtube videos started loading inside web view.

like image 103
Rashmi Ranjan mallick Avatar answered Sep 28 '22 03:09

Rashmi Ranjan mallick