YouTube的JSAPI,例如不工作 [英] Youtube JSAPI, examples not working

查看:117
本文介绍了YouTube的JSAPI,例如不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图捕捉YT对象。该onYouTubePlayerReady()函数的调用,但我似乎无法做的任何对象。

I'm trying to capture the YT object. The onYouTubePlayerReady() functions is called but I can't seem to do anything with the object.

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
    google.load("swfobject", "2.2");
</script>
<script type="text/javascript">
    function onYouTubePlayerReady (playerID)
    {
        ytplayer = document.getElementById(playerID);
    }

    function play ()
    {
        if (ytplayer)
        {
            ytplayer.playVideo();
        }
    }
    play();
</script>

YouTube播放器被引用到DOM,而不是影片本身(打为此抛出的未捕获类型错误:对象#有没有一种方法的playVideo,但这个基本上是由谷歌给出的例子。

ytplayer is referenced to the DOM and not the video itself (play therefor throws Uncaught TypeError: Object # has no method 'playVideo', but this is basically the example given by google.

我在做什么错了?

推荐答案

我没有看到你在上面code。在所有初始化您的视频播放器。下面的code是在Firefox工作的罚款。替换你想在地方低于[YOUR_VIDEO_ID]播放视频的视频ID,你应该准备就绪。还可以确认低于code就像在IE和Chrome一个冠军为好。

I don't see where you're initializing your video player at all in your above code. The code below is working fine in Firefox. Substitute the video ID of the video you would like to play in place of [YOUR_VIDEO_ID] below and you should be all set. Can also confirm that the code below works like a champ in IE and Chrome as well.

<div id="video" class="overlay">
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js"></script>
  <div id="ytapiplayer"></div>
  <script type="text/javascript">
    var params = { allowScriptAccess: "always" };
    var atts = { id: 'myytplayer' };
    swfobject.embedSWF("http://www.youtube.com/v/[YOUR_VIDEO_ID]?enablejsapi=1&playerapiid=ytplayer&version=3", "ytapiplayer", "640", "360", "8", null, null, params, atts);

    function onYouTubePlayerReady(playerId) {
      ytplayer = document.getElementById("myytplayer");

      ytplayer.addEventListener('onStateChange','onytplayerStateChange');
    }

    function onytplayerStateChange(newState) {
      alert("Player's new state: "+newState);
    }

    function play() {
      if (ytplayer) {
        ytplayer.playVideo();
      }
    }
  </script>
  <a href="javascript:void(0)" onclick="play()">Play</a>
</div>

我想你错过的是,你需要引用由无论你传递给对象的初始化属性(VAR的ATT = ...)行playerId。做测试的一点点也表明了onYouTubePlayerReady功能不实际的pass值的有效ID,可用于执行DOM查找。

I think what you're missing is that you need to reference the "playerId" by whatever you passed in to the object initialization attributes (var atts = ...) line. Doing a little bit of testing does reveal that the "onYouTubePlayerReady" function does not actual pass a valid id value that can be used to perform the DOM lookup.

这篇关于YouTube的JSAPI,例如不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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