2011-02-23 345 views
0

我有一個網站,並在主頁上我有一個嵌入的YouTube視頻,即包含玩家最初是隱藏在div(顯示:無),因此,一個按鈕(視頻)被點擊時,我隱藏頁面的內容並顯示YouTube播放器,所有工作都可以,但是當點擊按鈕(視頻)時,播放器出現並開始自動再現視頻,但我很喜歡它,但是我在每次嘗試中都出現了錯誤,我使用的是我在其他網站找到一個代碼,並做了一些改動自動播放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
謝謝所有

回答

0

好,讀的Youtube JavaScript播放器的API參考和編碼了一會兒後,這個工作對我來說:在ytplayer_statechange功能

,我改變了狀態的驗證,以

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

視頻開始1秒後繼續播放。我不知道這是否是最好的方式,但對我來說就足夠了。

+0

Youtube如何繞過WebKit HTML5 video'muted'? – moeseth 2017-08-17 16:33:36