javascript - 在 iOS 上使用 javascript api 嵌入 vimeo

标签 javascript ios api vimeo

我正在尝试使用 vimeo 的 javascript api ( http://developer.vimeo.com/player/js-api ) 通过我的应用程序控制嵌入视频,但我什至无法让他们的示例工作 ( http://jsfiddle.net/bdougherty/HfwWY/light/ )

我尝试下载 fiddle 使用的 jquery 和 frogaloop javascript,并将它们复制到 xcode 中,我还确保将它们放在捆绑资源上,而不是像人们建议的那样放在编译源上。

在我的 ViewController 上,我有:

NSString *path = [[NSBundle mainBundle] pathForResource:@"vimeo" ofType:@"html"];
NSString *html = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

[_webby loadHTMLString:html baseURL:[[NSBundle mainBundle] bundleURL]];

然后我的 vimeo.html 看起来像这样

<html>
<body>
<iframe id="player1" src="http://player.vimeo.com/video/27855315?api=1&player_id=player1" width="400" height="225" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>

<p>Video status: <span class="status">...</span></p>
<p><button>Play</button> <button>Pause</button></p>

<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="froogaloop2.min.js"></script>
<script type="text/javascript" src="player.js"></script>

</body>

其中jquery.min.jshttps://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.jsfroogaloop2.min.jshttp://a.vimeocdn.com/js/froogaloop2.min.js

然后我的 player.js 就像带有 ondomready 函数的 fiddle 示例:

$(document).ready(function () {
var iframe = $('#player1')[0],
    player = $f(iframe),
    status = $('.status');

// When the player is ready, add listeners for pause, finish, and playProgress
player.addEvent('ready', function() {
    status.text('ready');

    player.addEvent('pause', onPause);
    player.addEvent('finish', onFinish);
    player.addEvent('playProgress', onPlayProgress);
});

// Call the API when a button is pressed
$('button').bind('click', function() {
    player.api($(this).text().toLowerCase());
});

function onPause(id) {
    status.text('paused');
}

function onFinish(id) {
    status.text('finished');
}

function onPlayProgress(data, id) {
    status.text(data.seconds + 's played');
}
 });

文档还指出:“您需要在 Web 服务器上运行,而不是直接在浏览器中打开文件。Flash 和 JS 安全限制将阻止 API 在本地运行时工作。”

不确定如何不直接使用浏览器打开js,正如有人建议我尝试更改我的ViewController上的这一行:

[_webby loadHTMLString:html baseURL:[[NSBundle mainBundle] bundleURL]];

像这样:

[_webby loadHTMLString:html baseURL:[NSURL URLWithString:@"http://player.vimeo.com/"]];

对我来说也不起作用。

最佳答案

我也遇到了类似的问题。我能够做到这一点:

  1. 您必须按照您提到的那样使用 http://player.vimeo.com 设置基本 URL,否则它将无法工作。

  2. 我无法让它引用本地 js 文件。我必须使用远程源。我的意思是:

    <script src="http://f.vimeocdn.com/js/froogaloop2.min.js"></script>
    
  3. 我没有使用过jquery,我摆脱了那个代码。基本上我的代码如下所示:

        var iframe = document.getElementById('player1');
        var player = $f(iframe);
    
        player.addEvent('ready', function()
        {
            sendNotification('onReady', '');
    
            player.addEvent('pause', onPause);
            player.addEvent('finish', onFinish);
            player.addEvent('playProgress', onPlayProgress);
        });
    
        function onPause(id)
        {
            sendNotification('onStateChange', 'paused');
        }
    
        function onFinish(id)
        {
            sendNotification('onStateChange', 'finished');
        }
    
        function onPlayProgress(data, id)
        {
            sendNotification('onPlayTime', data.seconds);
        }
    
        function sendNotification(action, data)
        {
            var message = {};
    
            message['action'] = action;
            message['data'] = data;
    
            window.webkit.messageHandlers.observe.postMessage(message);
        }
    

关于javascript - 在 iOS 上使用 javascript api 嵌入 vimeo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24195992/

相关文章:

javascript - 如何在选择输入中自动选择下拉列表的第一个值?

javascript - 在 Javascript 中将一个值与数组中的所有值进行比较

javascript - 模态 Laravel 5.1

javascript - 如何: push() function in js not inserting value

iOs:(Swift 3)我用 var 变量初始化:[[Any]] = [],但出现错误消息 [type 'Any' has no subscript members]

ios - JSONSerialization.jsonObject 返回 nil

php - 通过 URL 的 JSON 响应不显示

ios - 推送通知 iOS 添加设备

javascript - API、.factory、.controller 错误

javascript - 使用 Google map API 添加多个标记