2015-03-03 92 views
0

我無法獲得對YouTube播放列表視頻的參考。獲取對YouTube播放列表的參考

這不起作用JSFIDDLE

我甚至不知道它的真正使在播放列表JSAPI?

var tag = document.createElement('script'); 
tag.src = "https://www.youtube.com/iframe_api"; 
var firstScriptTag = document.getElementsByTagName('script')[0]; 
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); 

var player; 
function onYouTubeIframeAPIReady() { 
    player = new YT.Player('movie_player', { 
    events: { 
     'onReady': onPlayerReady, 
     'onStateChange': onPlayerStateChange 
    } 
    }); 
} 

function onPlayerReady() { 
    console.log("hey Im ready"); 
//do whatever you want here. Like, player.playVideo(); 

} 

function onPlayerStateChange() { 
    console.log("my state changed"); 
} 
+0

你究竟想用YouTube播放列表做什麼? – 2015-03-03 11:00:58

+0

我想在我想要的時候停止播放視頻,例如,每10分鐘播放一段視頻,然後繼續播放視頻。我認爲我需要從播放列表中播放視頻。 – 2015-03-03 11:07:36

回答

2

我認爲下面的代碼可以幫助您開始,因爲我沒有正確理解您想要實現的目標。但我知道你需要播放和暫停視頻。所以,下面是我在我的項目 - 地方使用的代碼

function toggleVideo(state) { 
     if(state == 'pause'){ 
      document.getElementById('movie_player').contentWindow.postMessage('{"event":"command","func":"pauseVideo","args":""}', '*'); 
     } 
     else { 
      document.getElementById('movie_player').contentWindow.postMessage('{"event":"command","func":"playVideo","args":""}', '*'); 
     } 
    } 

UPDATE 我已經完全明白,現在下面的步驟是如何可以實現公司招聘

1)獲取JSON數據使用代碼播放列表如下─

var playlistId = 'PL1HIp83QRJHRbmJl0sS0CLOuIKCBCB76L'; 
$.getJSON('https://gdata.youtube.com/feeds/api/playlists/'+ playlistId +'?v=2&alt=jsonc',function(data,status,xhr){ 
// save json data of the playlist 
}).error(function() { alert("404 Error"); }); 

看到以下網址的XML格式,你可以通過URL-的API使用「& ALT = jsonc」獲得JSON格式相同的數據https://developers.google.com/youtube/2.0/developers_guide_protocol_playlists#Retrieving_a_playlist

2)創建兩個你需要保留兩個youtube播放器的iframe或容器。第一個是你應該每隔10分鐘左右暫停一次的球員,暫停時你應該隱藏第一個容器。第二種情況應該持有應該播放完畢的第二個視頻的播放器,完全隱藏該容器並再次顯示第一個容器。

參見如何玩這個職位和暫停使用YouTube videos- How to pause a YouTube player when hiding the iframe?

3)每次10分鐘左右後,您可以通過播放列表的JSON數據的下一個視頻鏈接和細節,並將其鏈接到第二容器暫停&隱藏第一個和播放&顯示第二個。

我希望通過這種方式你可以達到你的要求。

+0

我試過了[jsfiddle](https://jsfiddle.net/50w9s8ms/)它不起作用。 無論播放列表中的視頻是什麼,我都需要停止播放視頻。只需點擊「停止視頻」,任何播放視頻都會停止。這是真的嗎? – 2015-03-03 11:26:32

+0

但由於真實原因,我希望該視頻每隔10分鐘停止一次,它會在前一個視頻下顯示另一個視頻。好的,例如它是這樣的。 然後當第二個視頻結束時,關閉它。然後隨着播放時間停止播放第一個視頻。 – 2015-03-03 11:31:25

+0

@slokerantonov請看我更新的答案,這應該會讓你走上正軌 – 2015-03-03 12:45:13