YouTube iFrame API .seekTo() 不是方法? [英] YouTube iFrame API .seekTo() not a method?

查看:13
本文介绍了YouTube iFrame API .seekTo() 不是方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 .seekTo() 在 Javascript API 中工作,但我无法让它与 iFrame API 一起工作.支持这种方法吗?下面的代码成功嵌入视频并成功 console.log-s 播放器对象.我在控制台中收到错误player.seekTo 不是函数".

I know that .seekTo() works in the Javascript API, but I'm not able to get it working with the iFrame API. Is this method supported? The code below successfully embeds the video and successfully console.log-s the player object. I get the error "player.seekTo is not a function" in the console.

    <script>
        var tag = document.createElement('script');
        tag.src = "http://www.youtube.com/player_api";
        var firstScriptTag = document.getElementsByTagName('script')[0];
        firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
        var player;
        function onYouTubePlayerAPIReady() {
            player = new YT.Player('player', {
                height: '390',
                width: '640',
                videoId: 'u1zgFlCw8Aw'
            });
        }
        $(window).load(function(){
            jQuery('#test').click(function(){
                //console.log('test');
                console.log(player);
                player.seekTo(25);
                return false;
            });
        });
    </script>
    <a href="#" id="test">Test</a>

有什么想法吗?谢谢!

推荐答案

这里是更新的 jsfiddle 使用新的 iframe api作品

Here's an updated jsfiddle using the new iframe api that works

仅供参考:如果您只是使用 iframe 纯 HTML 嵌入,您可以将 ?start=30 作为开始时间

FYI: If you're just using an iframe pure HTML embed you can put ?start=30 for the start time

<iframe width="640" height="390" 
        src="//www.youtube.com/embed/p2H5YVfZVFw?start=30" 
        frameborder="0" allowfullscreen></iframe>

对于 API,您可以像这样在特定时间启动视频.使用 start 参数

For the API you can start a video at a certain time like this. Use the start parameter

function onYouTubePlayerAPIReady() {
     player = new YT.Player('player', {
       height: '390',
       width: '640',
       videoId: 'p2H5YVfZVFw',
         playerVars: { 'start': 159, 'autoplay': 1, 'controls': 1 },
       events: {
         'onReady': onPlayerReady,
         'onStateChange': onPlayerStateChange,
       }

     });   

您只能在播放器开始播放或不执行任何操作后调用 seekTo.检查 playerStateChange 回调:

You can only call seekTo after the player has started playing or it doesn't do anything. Check the playerStateChange callback :

onStateChange': onPlayerStateChange

添加这个回调函数

function onPlayerStateChange(evt) 
{       
    if (evt.data==1) 
    {
        // this will seek to a certain point when video starts
        // but you're better off using 'start' parameter
        // player.seekTo(22);   
    }
}

jsFiddle 底部有按钮,用于寻找 30、60、90 秒.它已经在所有与 'rome' 押韵的浏览器上进行了测试.当它打开时,它会为 player.seekTo 函数类型设置一个警告框.如果这显示未定义",则说明有问题.

The jsFiddle has buttons at the bottom to seek to 30, 60, 90 seconds. It has been tested with all browsers that rhyme with 'rome'. When it opens it puts up an alert box for the player.seekTo function type. If this shows 'undefined' you have a problem.

这篇关于YouTube iFrame API .seekTo() 不是方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆