目标c - 如何使用JS自动播放Vimeo视频

Eya*_*yal 6 javascript iphone objective-c vimeo ios

每个试图让Youtube/Vimeo视频在iOS上自动播放的人都知道这可能是一项痛苦的任务.Apple出于正确的原因阻止了'autoplay'参数,但有时您仍然需要使此功能正常工作.

我有同样的问题,自动播放youtube视频,显然,要让自动播放工作,你需要做一些javascript魔术,听取播放器的'onReady'事件,而不是当播放器加载并准备播放你打电话' player.play()'在没有任何其他用户干预的情况下启动它.

Vimeo也有一些javascript API,我很确定你可以用它做自动播放技巧,我只是无法弄清楚如何使用它.

他们有JS迷你库Froogaloop,让事情变得更容易,我看到@ila的这个答案与下面的html字符串一起使用它:

NSString *htmlString = [NSString stringWithFormat:
                                                @"<html><head>"
                                                "<script src=\"froogaloop.js\"></script>"
                                                " <script>"
                                                    "function ready()"
                                                        "{$f(document.getElementById(\"player\")).api(\"play\");}"
                                                    "function func1() "
                                                        "{$f(document.getElementById(\"player\")).addEvent(\"ready\", ready);}"
                                                    "window.onload=func1;"
                                                "</script></head><body>"
                                                       "<iframe id=\"player\" src=\"http://player.vimeo.com/video/39287488?api=1&amp;player_id=player\" width=\"315\" height=\"455\" frameborder=\"0\" webkit-playsinline>"
                                               " </iframe></body></html>"];    

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSLog(@"webview loaded");
    NSString *path = [[NSBundle mainBundle] pathForResource:@"froogaloop" ofType:@"js"];
    NSString *jsCode = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    [webView stringByEvaluatingJavaScriptFromString:jsCode];
}
Run Code Online (Sandbox Code Playgroud)

但这对我来说不起作用,在向这段代码添加警报之后,我可以看到func1()调用并执行'addEvent'行,但是它接缝处理的ready()方法永远不会被调用,因为'ready'事件从未触发过(?) ...

有任何想法吗?

Jes*_*edc 8

我对这个问题的第一个答案是一种不太理想的蛮力方法.我建议使用Vimeo Javascript API和froogaloop查看我的第二种方法.

我将假设您在html页面中使用Vimeo iframe,并且您在iOS应用程序内或Safari内部的UIWebView中使用此页面.

对Vimeo iframe的一点点反省表明它最初没有加载html视频标签.您将不得不以编程方式点击"播放"按钮来加载视频.

为了在今天(2013年4月,它可能会改变)这样做,你可以抓住正确的元素并调用正确的功能.最好用一些工作示例代码进行演示.

// You are loading this page inside a UIWebView
<html>
<head></head>
<body>
    <iframe width="" height="" src="http://player.vimeo.com/video/62207569 " frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
    <script>
    // Add a listener for the window's load event
    window.addEventListener('load', onWindowLoad, false);

    function onWindowLoad(event) {
        //use a loop to continue checking the vimeo iframe for the 'play' button
        checkForTrue(isIframeButtonLoaded, iFrameButtonLoaded);
    }

    function checkForTrue(func, callback) {
        setTimeout(function() {
            if (func()) {
                callback( iFrameButton() );
            } else {
                checkForTrue(func, callback);
            };
        }, 1000);
    }

    //finds the 'play' button within the Vimeo iframe
    function iFrameButton() {
                    //window.frames[0].document.getElementsByTagName('div')[1]; // this also works...
        return window.frames[0].document.getElementsByTagName('button')[5];
    }

    //simple boolean condition to check for the iframe button
    function isIframeButtonLoaded() {
        return ( iFrameButton() != null );
    }

    //once the button is loaded, click it
    function iFrameButtonLoaded(iFrameButton) {
        iFrameButton.click();
    }

    </script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

此源代码也可作为要点提供

一个理智的人可能会说这不是最好的方法,但为了在你的iOS应用程序中自动播放vimeo视频,它似乎确实有效.

从bundle中加载html文件并将其加载到UIWebView中的代码是样板:

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"VimeoTrial" ofType:@"html"];
    NSString *htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];

    [self.webView loadHTMLString:htmlString baseURL:nil];
}
Run Code Online (Sandbox Code Playgroud)