ios - YouTube 播放器的事件在 UIWebView 中不起作用

标签 ios uiwebview youtube

我想自动播放电影并在 UIWebView 上自动播放下一部电影。我在 UIWebView 上嵌入了 YouTube 播放器并使用了一些 YouTube Player API for iFrame片段,但它们效果不佳。

HTML 如下。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<style type="text/css">
    body {
        background-color: #000;
        margin: 0;
    }
</style>
</head>
<body>
<div id="player"></div>

<script>
  var tag = document.createElement("script");
  tag.src = "http://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: "UqFvrjhbO8c",
      events: {
        "onReady": onPlayerReady,
        "onStateChange": onPlayerStateChange
      }
    });
  }

  function onPlayerReady(event) {
    event.target.playVideo();
  }

  function onPlayerStateChange(event) {
    if (event.data == YT.PlayerState.ENDED) {
      document.location = "api://didEndedMovie";
    }
  }
</script>
</body>
</html>

电影结束时,我希望加载 webview api://didEndedMovie .然后,WebView 委托(delegate)将接收到加载的事件,并调用下面的委托(delegate)方法。
- (void)viewDidLoad
{
[super viewDidLoad];

if (!_htmlTemplate) {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"YouTubePlayer" ofType:@"html"];
    _htmlTemplate = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
}
NSString *htmlString = [NSString stringWithFormat:_htmlTemplate];
[self.webView loadHTMLString:htmlString baseURL:nil];
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString *requestString = [[request URL] absoluteString];

// if delegate received requests like "api://*", this view controller
// will not move to other views.  

if ([requestString rangeOfString:@"api://"].location == NSNotFound) {
    return YES;
}

// called when the movie ended
if ([requestString isEqualToString:@"api://didEndedMovie"]) {
    NSLog(@"didEndedMovie");
}

return NO;
}

但是,javascript 不起作用...事件 onReadyonStateChange不被解雇。

在浏览器上,这个 javascript 运行良好。

为什么不触发事件?

最佳答案

这是一个较老的问题,我不知道您是否还需要帮助,所以我会简明扼要。

不要使用 [webView loadHTMLString:baseURL],而是使用 [webView loadRequest]。 loadRequest 方法能够完全加载引用的 YouTube iframe javascript,并且您能够与 iframe api 交互。然后您将看到 onReady 和 onStateChange 事件触发。您还可以调用提示函数和其他东西。如果这不能回答问题,请告诉我。

关于ios - YouTube 播放器的事件在 UIWebView 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13548370/

相关文章:

ios - 移除 UIWebView 的内部缓存

ios - iPhone : How to play a YouTube video in a certain view (instead of fullscreen)

iOS - 无法使用 APNS 证书创建新的配置文件

iphone - iPad 通用的 xib 文件问题

iOS 键盘 - 使用自动布局输入附件 View xib 未接收触摸事件

objective-c - UIWebview中如何获取超链接的坐标

iOS OAuth 1.0 requestToken 用户授权 : how do I detect when a UIWebView is done with the authorizing?

javascript - 转换Javascript Image Rotator播放Youtube视频

youtube - YouTube在Android TV上的意图

ios - 如何在 Objective-C 中从 HTML 读取 JSON-LD 数据?