即使用document.getElementById('xyz')也无法控制Youtube嵌入.playVideo() - 不是函数?

Amy*_*Amy 3 javascript flash youtube-api embedded-resource

好吧,我被卡住了,即使在关注了Google的文档并在Stackoverflow上阅读建议之后我也不知道出了什么问题.为什么我无法在网页中控制Youtube嵌入?

如果我创建一个HTML文件,其中<body>是:

<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>
Run Code Online (Sandbox Code Playgroud)

即使我试图这样做:

// I get an object. Yay.

document.getElementById('e1');

// This generates "...playVideo is not a function"

document.getElementById('e1').playVideo();
Run Code Online (Sandbox Code Playgroud)

救命!我究竟做错了什么?谢谢.

Amy*_*Amy 5

好的,所以这里是API页面上一小段文字的答案:http://code.google.com/apis/youtube/js_api_reference.html

"注意:要测试这些调用中的任何一个,您必须在Web服务器上运行文件,因为Flash播放器会限制本地文件和Internet 之间的调用."

因此,为了让我继续在Mac笔记本电脑上进行开发,我做了以下工作:

  1. 编辑我的/ etc/hosts文件以包含一个返回我的localhost的条目:

    127.0.0.1   testhost.com
    
    Run Code Online (Sandbox Code Playgroud)
  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>
    
    Run Code Online (Sandbox Code Playgroud)
  3. 重启Apache:

    sudo apachectl restart
    
    Run Code Online (Sandbox Code Playgroud)
  4. 通过我的新虚拟服务器浏览回我自己的localhost:

    http://testhost.com
    
    Run Code Online (Sandbox Code Playgroud)

瞧.这完全有效.我可以查询播放器的页面:

document.getElementById('e1');                // OK
document.getElementById('e1').playVideo();    // OK!
Run Code Online (Sandbox Code Playgroud)

呼!没有onYouTubePlayerReady()也需要!