YTPlayer将重定向到OAuth2网址并显示“请等待”。

时间:2015-05-27 12:20:37

标签: ios objective-c youtube-api youtube-livestreaming-api

我试图在iOS App中使用YTPlayer播放实时流。

我使用简单的代码,只更改了' videoId'到了' w-T2LJ_qLiw'这是YouTube上的实时视频。

但是当加载视频时,应用会打开Safari并重定向到URL,如下所示:

https://accounts.google.com/o/oauth2/postmessageRelay?parent=https%3A%2F%2Fwww.youtube.com#rpctoken=840721360&forcesecure=1

https://content.googleapis.com/static/proxy.html?jsh=m%3B%2F_%2Fscs%2Fabc-static%2F_%2Fjs%2Fk%3Dgapi.gapi.en.dPxK-DAj_pE.O%2Fm%3D__features__%2Fam%3DAAQ%2Frt%3Dj%2Fd%3D1%2Frs%3DAItRSTN0fuoBkyFaoHWfzWWLct0BxZgQSQ#parent=https%3A%2F%2Fwww.youtube.com&rpctoken=2021820196

然后视频将被加载并播放,但覆盖的将是黑色块显示'请等待。'

如果视频不是“直播”,则视频效果会很好。

我昨天才尝试过,效果很好。 YTPlayer发生了什么? 我需要通过代码控制视频的动作,如播放,暂停和重新加载 有什么事可以解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

得到解决方案。

通过修改方法停止App跳出

- (BOOL)handleHttpNavigationToUrl:(NSURL *) url

来自YTPlayerView.m。

更改为以下内容:

if (ytMatch || adMatch) {  
     return YES;  
  } else {  
//    [[UIApplication sharedApplication] openURL:url];  
//    return NO;  
      return YES;  
  }  

或删除此方法并只写

return YES

这种方式没有'请等待......'展示。

答案 1 :(得分:0)

虽然周义棠的解决方案可行,但它打破了YouTube的服务条款。我已经修改了handleHttpNavigationToUrl:方法来检查这两个额外的URL,然后再将用户从UIWebView带到Safari中。

这是我的工作解决方案:

// YTPlayerView.m

// ...

NSString static *const kYTPlayerOAuthRegexPattern = @"^http(s)://accounts.google.com/o/oauth2/(.*)$";
NSString static *const kYTPlayerStaticProxyRegexPattern = @"^https://content.googleapis.com/static/proxy.html(.*)$";

// ...

- (BOOL)handleHttpNavigationToUrl:(NSURL *) url {
  // Usually this means the user has clicked on the YouTube logo or an error message in the
  // player. Most URLs should open in the browser. The only http(s) URL that should open in this
  // UIWebView is the URL for the embed, which is of the format:
  //     http(s)://www.youtube.com/embed/[VIDEO ID]?[PARAMETERS]
  NSError *error = NULL;
  NSRegularExpression *ytRegex =
      [NSRegularExpression regularExpressionWithPattern:kYTPlayerEmbedUrlRegexPattern
                                                options:NSRegularExpressionCaseInsensitive
                                                  error:&error];
  NSTextCheckingResult *ytMatch =
      [ytRegex firstMatchInString:url.absoluteString
                        options:0
                          range:NSMakeRange(0, [url.absoluteString length])];

  NSRegularExpression *adRegex =
      [NSRegularExpression regularExpressionWithPattern:kYTPlayerAdUrlRegexPattern
                                                options:NSRegularExpressionCaseInsensitive
                                                  error:&error];
  NSTextCheckingResult *adMatch =
      [adRegex firstMatchInString:url.absoluteString
                        options:0
                          range:NSMakeRange(0, [url.absoluteString length])];

  NSRegularExpression *oauthRegex =
      [NSRegularExpression regularExpressionWithPattern:kYTPlayerOAuthRegexPattern
                                              options:NSRegularExpressionCaseInsensitive
                                                error:&error];
  NSTextCheckingResult *oauthMatch =
    [oauthRegex firstMatchInString:url.absoluteString
                           options:0
                             range:NSMakeRange(0, [url.absoluteString length])];

  NSRegularExpression *staticProxyRegex =
    [NSRegularExpression regularExpressionWithPattern:kYTPlayerStaticProxyRegexPattern
                                              options:NSRegularExpressionCaseInsensitive
                                                error:&error];
  NSTextCheckingResult *staticProxyMatch =
    [[staticProxyRegex firstMatchInString:url.absoluteString
                                  options:0
                                    range:NSMakeRange(0, [url.absoluteString length])];

  if (ytMatch || adMatch || oauthMatch || staticProxyMatch) {
    return YES;
  } else {
    [[UIApplication sharedApplication] openURL:url];
    return NO;
  }
}

我还在项目的GitHub上建议将此更改为PR #111

编辑6/10/15:截至今天,此修改已合并到代码库中。