2012-02-15 103 views
6

我有一些YouTube嵌入代碼(我只會粘貼代碼,這會對我造成麻煩,並且剪掉不公開的東西):YouTube播放器的iframe API:播放視頻不能在Firefox 9.0.1上工作

console.log(ytplayer); 
ytplayer.playVideo(); 

Chrome和FF上的Console.log使用正確的方法向我顯示了很好的對象,並且方法playVideo()存在那裏。它適用於我檢查的所有其他瀏覽器,但它不適用於FF!?更有趣的是,當我使用普通的YouTube播放按鈕播放視頻時,則可以使用pauseVideo()方法(以及所有其他方法:搜索,控制音量),但我無法使用playVideo()方法...

我使用嵌入視頻的新方法:

ytplayer = new YT.Player(player, { 
     height: height, 
     width: width, 
     videoId: videoid, 
     allowfullscreen: 'true', 
     playerVars: { 
      controls: 0, 
      showinfo: 0, 
      wmode: 'opaque', 
      autoplay: (autoplay ? 1 : 0) 
     }, 
     events: { 
      'onReady': function() { 
       console.log('I am ready'); 
      } 
     } 
    }); 

當然,「我已經準備好」在控制檯輸出。我不知道我做錯了什麼,爲什麼只有FF不工作......沒有JS錯誤,也沒有線索......希望有人有過這個問題,並得到它解決!:)

+0

'沒有JS錯誤,也沒有線索只有問題頁面的鏈接可以幫助其他人識別問題,而不僅僅是你的話。 – Cheery 2012-02-15 04:11:44

+1

也許你是對的@Cheery,但我不能發佈你的鏈接,因爲我正在做的項目是紀念性的,並且必須是私人的,直到發佈......對不起!通過「沒有JS錯誤,也沒有線索」我的意思是,控制檯輸出中的所有內容對於Chrome和FF都是相同的,對於FF它不起作用。也沒有錯誤和警告。 – Karol 2012-02-15 04:45:22

+0

'也沒有錯誤和警告。「而且它需要'實驗'而不是'理論思考'。 – Cheery 2012-02-15 04:48:58

回答

9

我有一個非常類似的問題,並正在努力尋找答案。我打電話播放視頻()似乎沒有工作。

ORIGINAL:

$('#play_movie').click(function(){ 
    $('#video').show(); 
    if(player) 
    { 
     if(typeof player.playVideo == 'function') 
     { 
      player.playVideo(); 
     } 
    } 

的問題是,玩家還無法使用 - 如果我只是給它一點時間顯示出來,然後調用工作

$('#play_movie').click(function(){ 
    $('#video').show(); 
    if(player) 
    { 
     var fn = function(){ player.playVideo(); } 
     setTimeout(fn, 1000); 
    } 

別不知道這是否是你確切的問題,但我希望它可以幫助別人

+0

它可以是某些情況下的解決方案。 – Karol 2012-06-27 07:57:23

+3

玩家可能會觸發'onReady'事件,而不是'onYouTubeIframeAPIReady',您可以使用它,這似乎更正確。 – 2012-07-26 21:42:37

+0

在Chrome中,'playVideo()'方法適用於我,但在FF和IE中失敗。 IE報告錯誤,該對象不支持該方法。一旦我添加超時,它就像一個魅力。謝謝! – gtr1971 2013-02-13 18:58:10

7

更強大的方法是檢查玩家是否準備好。如果播放器未準備就緒,請將player.playVideo()放入隊列中,並在準備好使用onReady事件時執行它。 Gist

var playerConfig = {},     // Define the player config here 
    queue = {       // To queue a function and invoke when player is ready 
     content: null, 
     push: function(fn) { 
     this.content = fn; 
     }, 
     pop: function() { 
     this.content.call(); 
     this.content = null; 
     } 
    }, 
    player; 

window.onYouTubeIframeAPIReady = function() { 
    player = new YT.Player('player', { 
    videoId: 'player', 
    playerVars: playerConfig, 
    events: { 
     onReady: onPlayerReady 
    } 
    }); 
}; 

// API event: when the player is ready, call the function in the queue 
function onPlayerReady() { 
    if (queue.content) queue.pop(); 
} 

// Helper function to check if the player is ready 
function isPlayerReady(player) { 
    return player && typeof player.playVideo === 'function'; 
} 

// Instead of calling player.playVideo() directly, 
// using this function to play the video. 
// If the player is not ready, queue player.playVideo() and invoke it when the player is ready 
function playVideo(player) { 
    isPlayerReady(player) ? player.playVideo() : queue.push(function() { 
               player.playVideo(); 
              }); 
} 
+0

我也喜歡這種方法。從來沒有這樣想過 - 像你說的那樣整齊而健壯。 – Karol 2014-08-28 00:44:14

+0

應該接受答案!如果我不得不抱怨你的語義,我會說你的隊列不是一個真正的隊列(混淆了push/pop),但是你所做的真的很好。 – 2015-09-24 23:19:17

1

我遇到這個帖子尋找類似的東西。我發現我的答案在這裏,通過relic180:

YouTube API - Firefox/IE return error "X is not a function" for any 'player.' request

基本上,Chrome瀏覽器可以初始化YouTube嵌入即使div的隱藏(即display:none),但FF和IE不能。我的解決辦法relic180的的變體:

我將我的球員left:200%或者當我想它無形但要初始化(並可供其他呼叫player),然後將其移回在屏幕上時,我需要什麼。

0

我個人發現在預設IFrames上,如果您不使用https://www.youtube.com作爲域,API將無法正常工作。所以要小心不要錯過「www」。否則,API將創建播放器對象,但無法執行方法。

相關問題