2017-08-08 21 views
0

我想使用YouTube API從this tutorial構建自定義YouTube播放列表,並且我在某個時間點被卡住了。 我基本上嵌入了client.js腳本並在加載時執行它的功能,之後我也嵌入了教程中所述的YouTubePlayList.js文件。 這是我試圖做的一把小提琴。我確實收到了控制檯中的YouTubePlayList對象,但它似乎沒有提供任何正確的數據。我需要一個有效的腳本示例或指導,說明如何實現它並使播放列表呈現在我的客戶端。在此先感謝,任何幫助表示讚賞!生成YouTube自定義播放列表問題

JS:

<pre> 

function YouTubePlayList (id, entries) { 
    this.id = id; 
    this.entries = entries; 
    this.currently_playing = 0; 
    this.randomizer = false; 
} 
var requestOptions = { 
    playlistId: 'PLLzJfby7cTLTbusOgXca-yIpVOImC1mWe', 
    part: 'contentDetails, snippet', 
    execute: function(response) { 
     var entries = []; 
     $.each(response.items, function(key, val){ 
      var entry = {}; 
      entry.video_id = val.snippet.resourceId.videoId; 
      entry.image_src = val.snippet.thumbnails.medium.url; 
      entry.title = val.snippet.title; 
      entry.note = val.contentDetails.note; 
      entries.push(entry); 
     }); 
    } 
}; 
window['PLLzJfby7cTLTbusOgXca-yIpVOImC1mWe'] = new YouTubePlayList('PLLzJfby7cTLTbusOgXca-yIpVOImC1mWe', 1); 
console.log(window['PLLzJfby7cTLTbusOgXca-yIpVOImC1mWe']); 

</pre> 

回答

2

您可以訪問Playlists: insert

這將幫助您在您的頻道新的播放列表。這個頁面上塞滿了想法,可以幫助你開始。還有一些例子,如下面的.js代碼。

// Define some variables used to remember state. 
var playlistId, channelId; 

// After the API loads, call a function to enable the playlist creation form. 
function handleAPILoaded() { 
    enableForm(); 
} 

// Enable the form for creating a playlist. 
function enableForm() { 
    $('#playlist-button').attr('disabled', false); 
} 

// Create a private playlist. 
function createPlaylist() { 
    var request = gapi.client.youtube.playlists.insert({ 
    part: 'snippet,status', 
    resource: { 
     snippet: { 
     title: 'Test Playlist', 
     description: 'A private playlist created with the YouTube API' 
     }, 
     status: { 
     privacyStatus: 'private' 
     } 
    } 
    }); 
    request.execute(function(response) { 
    var result = response.result; 
    if (result) { 
     playlistId = result.id; 
     $('#playlist-id').val(playlistId); 
     $('#playlist-title').html(result.snippet.title); 
     $('#playlist-description').html(result.snippet.description); 
    } else { 
     $('#status').html('Could not create playlist'); 
    } 
    }); 
} 

// Add a video ID specified in the form to the playlist. 
function addVideoToPlaylist() { 
    addToPlaylist($('#video-id').val()); 
} 

// Add a video to a playlist. The "startPos" and "endPos" values let you 
// start and stop the video at specific times when the video is played as 
// part of the playlist. However, these values are not set in this example. 
function addToPlaylist(id, startPos, endPos) { 
    var details = { 
    videoId: id, 
    kind: 'youtube#video' 
    } 
    if (startPos != undefined) { 
    details['startAt'] = startPos; 
    } 
    if (endPos != undefined) { 
    details['endAt'] = endPos; 
    } 
    var request = gapi.client.youtube.playlistItems.insert({ 
    part: 'snippet', 
    resource: { 
     snippet: { 
     playlistId: playlistId, 
     resourceId: details 
     } 
    } 
    }); 
    request.execute(function(response) { 
    $('#status').html('<pre>' + JSON.stringify(response.result) + '</pre>'); 
    }); 
} 

嘗試探索YouTube Player API Reference for iframe Embeds

IFrame播放器API可讓您在您的 網站上嵌入YouTube視頻播放器,並使用JavaScript控制播放器。

使用API​​的JavaScript函數,可以將視頻排列爲 播放;播放,暫停或停止這些視頻;調整播放器音量; 或檢索有關正在播放的視頻的信息。您還可以添加 事件偵聽器,這些事件偵聽器將響應某些播放器 事件執行,例如播放器狀態更改或視頻播放質量 更改。

本指南介紹瞭如何使用IFrame API。它標識API可以發送的不同類型的事件,並解釋如何編寫事件偵聽器來響應這些事件。它還詳細說明了不同的JavaScript函數,您可以調用不同的JavaScript函數來控制視頻 播放器以及播放器參數,您可以使用這些參數進一步 自定義播放器。

+1

非常感謝你的回答,它真的幫助我找到了另一個很棒的視頻資源,我發現它是[這個](https://www.youtube.com/watch?v=jdqsiFw74Jk).. 。在這裏,您還可以瞭解您實際需要發送的請求,如何發送它們,以及如何獲取需要在事後嵌入到個人網頁中的數據對象 – bookw0rm