代码之家  ›  专栏  ›  技术社区  ›  Amy

即使使用document.getElementByID(“xyz”).playVideo()也无法控制YouTube嵌入-不是函数?

  •  3
  • Amy  · 技术社区  · 15 年前

    好吧,我被卡住了,我不知道出了什么问题,即使在跟踪谷歌的文档和阅读StackOverflow上的建议之后。为什么我不能控制YouTube嵌入我的网页?

    如果我创建的HTML文件的正文为:

    <object id="o1" width="480" height="295">
      <param name="movie" 
        value="http://www.youtube.com/v/qCTLCNmnlKU&hl=en_US&fs=1&enablejsapi=1&">
      </param>
      <param name="allowFullScreen" value="true"></param>
      <param name="allowscriptaccess" value="always"></param>
      <embed id="e1" 
        src="http://www.youtube.com/v/qCTLCNmnlKU&hl=en_US&fs=1&enablejsapi=1&" 
        type="application/x-shockwave-flash" 
        allowscriptaccess="always" allowfullscreen="true" width="480" height="295">
      </embed>
    </object>
    

    即使我试图这样做:

    // I get an object. Yay.
    
    document.getElementById('e1');
    
    // This generates "...playVideo is not a function"
    
    document.getElementById('e1').playVideo();
    

    救命!我做错什么了?谢谢。

    4 回复  |  直到 10 年前
        1
  •  5
  •   Amy    15 年前

    好的,下面是在API页面上的一行文本中找到的答案: http://code.google.com/apis/youtube/js_api_reference.html

    “注意:要测试这些调用中的任何一个,您必须在Web服务器上运行您的文件,因为Flash播放器限制调用。” 在本地文件之间 还有互联网。”

    为了让我能够继续在我的Mac笔记本电脑上开发,我做了以下操作:

    1. 编辑我的 /ETC/主机 要包含返回本地主机的条目的文件:

      127.0.0.1   testhost.com
      
    2. 编辑我的 /etc/apache2/httpd.conf文件 要添加指向我的开发目录的虚拟主机条目的文件:

      <VirtualHost *:80>
          ServerName testhost.com
          DocumentRoot /Users/amy/flashproj
          <Directory /Users/amy/flashproj>
              AllowOverride all
              Options MultiViews Indexes FollowSymLinks
              Allow from All
          </Directory>
      </VirtualHost>
      
    3. 重新启动Apache:

      sudo apachectl restart
      
    4. 通过我的新虚拟服务器浏览回我自己的本地主机:

      http://testhost.com
      

    沃伊拉现在完全可以了。我可以查询玩家页面:

    document.getElementById('e1');                // OK
    document.getElementById('e1').playVideo();    // OK!
    

    唷!不 OnyOutubeplayerReady()。 也需要!

        2
  •  2
  •   Li0liQ    15 年前

    YouTube播放器在您尝试使用时尚未加载。有一个特殊的回调函数将在加载后自动激发。

    显示 无铬播放机必须实现 名为的回调函数 准备好了。API将 当玩家 完全加载并且API准备好 接收呼叫。

    通过 YouTube JavaScript Player API Reference .

    因此,可以用以下方式重写代码:

    <script type="text/javascript">
    function onYouTubePlayerReady(playerId) {
        var ytplayer = document.getElementById('e1');
        ytplayer.playVideo();
    }
    </script>
    

    你也可以通过 playerapiid 同时嵌入玩家,如果有足够多的玩家需要区分 onYouTubePlayerReady 处理程序。

        3
  •  0
  •   Pointy    15 年前

    如果使用ID“O1”而不是“e1”,会发生什么?

    好的,在这一页上: http://code.google.com/apis/youtube/js_api_reference.html 在我看来,“swfobject”库需要包含在其中才能使该API可用。我试试看。

    [编辑]好的,我有:

    <html>
      <head>
        <script src='http://code.jquery.com/jquery-1.4.1.js'></script>
        <script src='http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js'></script>
        <script>
          window['onYouTubePlayerReady'] = function onYouTubePlayerReady(playerId) {
            player = document.getElementById("zebra");
            player.playVideo();
          };
          $(function() {
             swfobject.embedSWF(
             "http://www.youtube.com/v/qCTLCNmnlKU?hl=en_US&fs=1&enablejsapi=1&playerapiid=ytplayer", 
              "foo",
              "480", "295",
              "8",
              null, null,
              { allowScriptAccess: 'always' },
              { id: 'zebra' }
            );
          });
        </script>
      </head>
      <body>
        <div id='foo'>Foo</div>
      </body>
    </html>
    

    这是可行的,但奇怪的是,它只在我从真正的Web服务器提供服务时才起作用。如果我把它放在一个本地文件中并加载到浏览器中,它就不工作了。我不知道为什么,但我绝对不是Flash专家,也不是YouTube专家。

    参见 http://gutfullofbeer.net/youtube.html (与上面输入的内容相同)

        4
  •  0
  •   Conan.Net    10 年前

    我被这个问题困扰了两天。与youtube播放器变量相关的每个问题都与在本地运行它相关。通过Windows/主机伪造地址后,它工作得很好。