【问题标题】:Vimeo not responding to Javascript in iOS appVimeo 没有响应 iOS 应用程序中的 Javascript
【发布时间】:2014-05-14 18:23:47
【问题描述】:

我正在尝试在 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)

这是工作小提琴的链接:http://jsfiddle.net/LunaEques/Hy43Z/

【问题讨论】:

标签: javascript jquery ios uiwebview vimeo


【解决方案1】:

我终于明白了。关键在于文档中的这一行:“您需要在 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 会正常运行。

【讨论】:

    猜你喜欢
    • 2020-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多