Youtube播放器控制&自动隐藏不起作用

时间:2014-04-30 02:33:41

标签: youtube-api

所以我将youtube iframe api添加到我的网站并按照说明操作;所以这是我的代码:

var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
  height: '390',
  width: '640', 
  controls: '0',
  autohide: '1',
  videoId: 'I'd rather not display that',
  events: {
//    'onReady': onPlayerReady,
    'onStateChange': onPlayerStateChange
  }
});
}

将控件设置为0不应按照this article显示播放器的控件,但它仍然显示;

同样将自动隐藏设置为1应该让进度条和控件按照this article在几秒后滑出,但这也不起作用。

我做错了吗?

2 个答案:

答案 0 :(得分:5)

我得到了嵌入版本和iframe版本的自动播放,自动隐藏(不需要)和隐藏控件。我使用了示例代码并添加了您需要的参数。

您需要做的就是更换您的电影ID,我认为您将启动并运行。

以下是jsfiddle链接,您可以在其中查看其工作并获取代码。

嵌入版本:http://jsfiddle.net/franktudor/jk9SS/

<object width="640" height="390">
  <param name="movie"
         value="https://www.youtube.com/v/M7lc1UVf-VE?version=3&autoplay=1&autohide=1&controls=0"></param>
  <param name="allowScriptAccess" value="always"></param>
  <embed src="https://www.youtube.com/v/M7lc1UVf-VE?version=3&autoplay=1&autohide=1&controls=0"
         type="application/x-shockwave-flash"
         allowscriptaccess="always"
         width="640" height="390"></embed>
</object>

以下是iFrame版本:http://jsfiddle.net/franktudor/2wh8T/

<iframe id="ytplayer" 
        type="text/html" 
        width="640" 
        height="390" 
        src="http://www.youtube.com/embed/M7lc1UVf-VE?autoplay=1&autohide=1&controls=0"
        frameborder="0"/>

两者都是您可以使用的选项。您正在使用iFrame版本...我也推荐该版本。但如果您需要其他解决方案,可以使用嵌入版本。

它不是脚本样式,但如果需要,您可以使用包装器。

Here is a link to an HTML5 youtube (and vimeo) video wrapper.

好的,这是我工作的例子中的功能代码:http://jsfiddle.net/franktudor/DU57E/

<div id="player"></div>

<script>
  // 2. This code loads the IFrame Player API code asynchronously.
  var tag = document.createElement('script');

  tag.src = "https://www.youtube.com/iframe_api";
  var firstScriptTag = document.getElementsByTagName('script')[0];
  firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

  // 3. This function creates an <iframe> (and YouTube player)
  //    after the API code downloads.
  var player;
  function onYouTubeIframeAPIReady() {
    player = new YT.Player('player', {
      height: '390',
      width: '640',
      videoId: 'M7lc1UVf-VE',
      playerVars: { 'autoplay': 1, 'controls': 0 }, //this is what you need...
      events: {
        'onReady': onPlayerReady,
        'onStateChange': onPlayerStateChange
      }
    });
  }

  // 4. The API will call this function when the video player is ready.
  function onPlayerReady(event) {
    event.target.playVideo();
  }

  // 5. The API calls this function when the player's state changes.
  //    The function indicates that when playing a video (state=1),
  //    the player should play for six seconds and then stop.
  var done = false;
  function onPlayerStateChange(event) {
    if (event.data == YT.PlayerState.PLAYING && !done) {
      setTimeout(stopVideo, 6000);
      done = true;
    }
  }
  function stopVideo() {
    player.stopVideo();
  }
</script>

答案 1 :(得分:0)

只需在属性'playerVars'中设置变量。

相关问题