OGeek|极客世界-中国程序员成长平台

标题: javascript - Vimeo 没有响应 iOS 应用程序中的 Javascript [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 22:16
标题: javascript - Vimeo 没有响应 iOS 应用程序中的 Javascript

我正在尝试在 iOS 应用程序中加载 vimeo 播放器,并且我需要能够通过 javascript 向其发送各种命令(播放、暂停、搜索)。我编写了一个脚本,它将加载和播放在我的浏览器中运行的视频,但是当我将相同的脚本加载到 UIWebView 并运行它时,视频加载但不播放。我不知道出了什么问题。我可以通过加载 youtube 视频并向其发送播放命令来获得我想要的行为,所以我不认为这是苹果禁用自动播放。

脚本:

          var playerExists = false;
      var f, playerURL;

      function loadPlayer(videoId) {
          playerURL = 'http://player.vimeo.com/video/' + videoId + '?api=1&player_id=player';
          //This changes the source of the iframe to the videoURL
          if (playerExists) {
              $('#player').attr('src', playerURL);
          } else {
              playerExists = true;
              $('<iframe id="player"/>').appendTo('body');
              $('#player').attr('src', playerURL);
              $('#player').load(function () {
                  ready('player');
              });
          }
          f = $('iframe');
      }

      // Helper function for sending a message to the player
      function post(action, value) {
          var data = {
              method: action
          };

          if (value) {
              data.value = value;
          }

          url = playerURL.split('?')[0];
          f[0].contentWindow.postMessage(JSON.stringify(data), url);
      }

      function ready(player_id) {
          playerReady = true;
          post('play');
      }

      loadPlayer(66979809)

这是工作 fiddle 的链接:http://jsfiddle.net/LunaEques/Hy43Z/



Best Answer-推荐答案


我终于明白了。关键在于文档中的这一行:“您需要在 Web 服务器上运行,而不是直接在浏览器中打开文件。Flash 和 JS 安全限制将阻止 API 在本地运行时工作。”

所以,问题是当我加载我的 javascript 代码时,我使用了

    NSURL* path = [[NSBundle mainBundle] URLForResource: @"vimeoPlayer"
                                          withExtension: @"html"];
    [self loadRequest: [NSURLRequest requestWithURL: path]];

这是在本地加载 html,从而禁用了 javascript。一旦我将其更改为

NSString *path = [[NSBundle mainBundle] pathForResource"vimeoPlayer"
                                                 ofType"html"];
NSString *content = [NSString stringWithContentsOfFile:path
                                              encoding:NSUTF8StringEncoding
                                                 error:nil];
[self loadHTMLString:content baseURL:[NSURL URLWithString"http://player.vimeo.com/"]];

javascript 会正常运行。

关于javascript - Vimeo 没有响应 iOS 应用程序中的 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23662268/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4