2012-06-07 33 views
3

我正在使用SWFObject將YouTube視頻嵌入到我們的網站中。有許多視頻鏈接在一個頁面,每一個環節清除包裝格,然後加載一個新的內嵌進去,與此代碼:如何正確使用SWFObject

$('a.video-link').each(function() { 
    $(this).on('click', function(e) { 
     e.preventDefault(); 

     if ($('#video-wrap').has('object').length == 0) { 
      var params = { allowScriptAccess: 'always', allowFullScreen: 'true' }; 
      var atts = { id: 'ytapiplayer' }; 
      swfobject.embedSWF($(this).attr('href'), 'ytapiplayer', '1000', '562', '8', null, null, params, atts); 
      $('#video-wrap').show(); 
     } else { 
      $('#video-wrap').find('object').remove(); 
      $(this).trigger('click'); 
     } 
    }); 
}); 

這是我使用的每一個嵌入Youtube的鏈接:

http://www.youtube.com/v/{Youtube ID}?hl=en_US&rel=0&hd=1&border=0&version=3&fs=1&autoplay=1&autohide=1&enablejsapi=1&playerapiid=ytapiplayer 

然後,這裏是onYouTubePlayerReady()事件處理程序:

function onYouTubePlayerReady(id) { 
    console.log('youtube player is ready.'); 
    var ytplayer = document.getElementById('ytapiplayer'); 
    ytplayer.addEventListener('onStateChange', 'onYouTubePlayerStateChange'); 
    ytplayer.addEventListener('onError', 'onYouTubePlayerError'); 
} 

所有視頻加載罰款,但是onYouTubePlayerReady永遠不會打了!

herehere但沒有

我試圖解決方案的工作:(

請幫我解決這個問題。的最終目標是讓YouTube API在工作。

感謝。

編輯:我試圖玩的代碼,確保所有名稱都正確,分開成不同的腳本標記和/或.js文件,加載它在開始,在document.ready()內,仍然,onYouTubePlayerReady是沒有t發射。 你覺得呢?

+0

後的SWFObject已嵌入播放器,檢查DOM節點並檢查設置allowscriptaccess =「總是」被設置正確。 – mqsoh

+0

它被正確設置...我得到它的工作,並將添加正確的代碼的答案:) – pilau

回答

3

這裏是工作代碼:

每個視頻鏈路上執行的SWFObject:

$('a.video-link').on('click', function(e) { 
    e.preventDefault(); 
    // SWFObjects loads a video object into div with ID ytapiplayer. 
    // If the wrapper div already contains a video we need to remove it first: 
    if ($('#video-wrap').has('object').length == 0) { 
     var params = { allowScriptAccess: 'always', allowFullScreen: 'true' }; 
     var atts = { id: 'ytapiplayer' }; 
     swfobject.embedSWF($(this).attr('href'), 'ytapiplayer', '1000', '562', '8', null, null, params, atts); 

     $('#video-wrap').show(); 
    } else { 
     $('#video-wrap').find('object').remove(); 
     $(this).trigger('click'); 
    } 
}); 

的YouTube鏈接與API值:SWFObject的事件處理程序

http://www.youtube.com/v/' + data.YoutubeLink + '?hl=en_US&rel=0&hd=1&border=0&version=3&fs=1&autoplay=1&autohide=1&enablejsapi=1&playerapiid=ytapiplayer 

而且我把這段代碼放在一個單獨的.js文件中,在執行SWFObject的代碼之前加載。我不知道這是否是必要的,但它的工作呢:

function onYouTubePlayerReady(id) { 
    // We need the actual DOM element for this, if we want to use more advanced API. 
    // This is because addEventListener activates the API. 
    var ytplayer = $('#ytapiplayer').get(0); 
    ytplayer.addEventListener('onStateChange', 'onYouTubePlayerStateChange'); // onYouTubePlayerStateChange(newState) 
    ytplayer.addEventListener('onError', 'onYouTubePlayerError'); // onYouTubePlayerError(errorCode) 
} 
+1

是的,這是必要的。在向YouTube提出任何請求之前,onYouTubePlayerReady應該可用。將來,您應該在embedSWF調用之前或之後手動調用函數來測試它。 – mqsoh

+0

這是一個好主意。事實上,我知道SWFObject需要在API調用之前定義onYouTubePlayerReady,事實上,在另一個網站中,我根本沒有任何問題。然而在這個項目中,我想將代碼分成兩個文件使其工作。 – pilau

+0

您可能沒有任何問題,因爲這是「競賽條件」。加載YouTube播放器的HTTP請求的時間正在加載JavaScript的瀏覽器。這是一個陰險的類型的錯誤。 – mqsoh