自动播放youtube视频

时间:2011-02-23 21:56:52

标签: flash video youtube

我有一个网站,在主页上我有一个嵌入的youtube视频,包含播放器的div最初是隐藏的(显示:无),因此,当点击一个按钮(视频)时,我隐藏了内容的页面和显示youtube播放器,一切正常,但我很乐意,当点击按钮(视频)时,播放器显示并开始自动重现视频,但我在每次尝试都假装,我使用我在其他网站上找到的代码并进行了一些更改

swfobject.addLoadEvent( ytplayer_render_player );
function ytplayer_render_player( )
{
swfobject.embedSWF
(
  'http://www.youtube.com/v/' + youtube_uhma.home + '&enablejsapi=1&rel=0&fs=1&playerapiid=ytplayer',
  'ytplayer_div1',
  '425',
  '344',
  '8',
  null,
  null,
  {
    allowScriptAccess: 'always',
    allowFullScreen: 'true'
  },
  {
    id: 'ytplayer_object'
  }
);
}  
 function onYouTubePlayerReady( playerid )
 {
    var o = document.getElementById( 'ytplayer_object' );
    if ( o )
    {
      o.addEventListener( "onStateChange", "ytplayer_statechange" );
      o.addEventListener( "onError", "ytplayer_error" );
    }
}
 function ytplayer_statechange( state )
 {
    if ( state == 0 )
    {
      ytplayer_playlazy( 5000 );
   }
 }

function ytplayer_error( error )
{
  if ( error )
  {
    ytplayer_playlazy( 5000 );
  }
 }

function ytplayer_playlazy( delay )
{
    if ( typeof ytplayer_playlazy.timeoutid != 'undefined' )
    {
      window.clearTimeout( ytplayer_playlazy.timeoutid );
    }
    ytplayer_playlazy.timeoutid = window.setTimeout( ytplayer_play, delay );
}
function ytplayer_play( )
{
  var o = document.getElementById( 'ytplayer_object' );
  if ( o )
  {
    o.loadVideoById( youtube_uhma.home );
  }
}

我发布了我正在使用的所有代码,因此,我如何在此处进行自动播放,您可以在此处查看我的网站Uhma
感谢所有

1 个答案:

答案 0 :(得分:0)

好吧,在阅读了You​​tube JavaScript播放器的API参考资料并编写了一段时间后,这对我有用:

ytplayer_statechange 函数中,我将状态验证更改为

if ( state == 5 ) //5 means that is ready to play
{
    ytplayer_playlazy( 1000 );
}

视频在1秒后开始播放。我不知道这是不是最好的方式,但对我来说已经足够了。