自动播放YouTube视频 [英] autoplay youtube video

查看:600
本文介绍了自动播放YouTube视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站,在主页上我有一个嵌入式YouTube视频,包含播放器的div最初是隐藏的(display:none),所以当点击一个按钮(视频)时,我隐藏了内容并显示YouTube的播放器,一切正常,但我会喜欢,当按钮(视频)被点击时,播放器出现,并开始自动重现视频,但我已经faild在每一次尝试,我' m使用一个代码,我发现在其他网络,并作出一些改变

  swfobject.addLoadEvent(ytplayer_render_player); 
函数ytplayer_render_player()
{
swfobject.embedSWF

'http://www.youtube.com/v/'+ youtube_uhma.home +'& ;
'425',
'344',
'8',
'425',
'
null,
null,
{
allowScriptAccess:'always',
allowFullScreen:'true'
},
{
id:'ytplayer_object'
}
);

函数onYouTubePlayerReady(playerid)
{
var o = document.getElementById('ytplayer_object');
if(o)
{
o.addEventListener(onStateChange,ytplayer_statechange);
o.addEventListener(onError,ytplayer_error);

$ b函数ytplayer_statechange(state)
{
if(state == 0)
{
ytplayer_playlazy(5000);



函数ytplayer_error(错误)
{
if(error)
{
ytplayer_playlazy(5000);



函数ytplayer_playlazy(延迟)
{
if(typeof ytplayer_playlazy.timeoutid!='undefined')
{
window.clearTimeout(ytplayer_playlazy.timeoutid);
}
ytplayer_playlazy.timeoutid = window.setTimeout(ytplayer_play,delay);

函数ytplayer_play()
{
var o = document.getElementById('ytplayer_object');
if(o)
{
o.loadVideoById(youtube_uhma.home);


$ / code $ / pre

我已经发布完整的代码,所以,我怎么能在这里自动播放,你可以看到我的网站在这里 Uhma

thanks to all

解决方案

好了,在阅读Youtube JavaScript播放器的API参考并编写了一段时间之后,对于我:在 ytplayer_statechange 函数中,



,我将状态验证更改为

  if(state == 5)// 5表示已准备好玩
{
ytplayer_playlazy(1000);
}

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


i have a site and in the home page i have an embedded youtube video, the div that is containing the player is initially hidden(display:none), so, when a button(video) is clicked, i hide the content of the page and show the youtube player, all works ok, but i would love that when the button(video) is clicked, the player shows up and start to reproduce the video automatically, but i've faild in each attempt, i'm using a code that i find in other web and made some changes

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 );
  }
}

i've post the entire code i'm using, so, how can i make autoplay here, you can see my web in action here Uhma
thanks to all

解决方案

well, after reading the API reference of the Youtube JavaScript player and coding for some while, this worked for me:

in the ytplayer_statechange function, I changed the validation of the state to

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

The video started to play after 1 sec. I don't know if this is the best way but it is enough for me.

这篇关于自动播放YouTube视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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