|
| 1 | +// Define some variables used to remember state. |
| 2 | +var playlistId, channelId; |
| 3 | + |
| 4 | +// After the API loads, call a function to enable the playlist creation form. |
| 5 | +function handleAPILoaded() { |
| 6 | + enableForm(); |
| 7 | +} |
| 8 | + |
| 9 | +// Enable the form for creating a playlist. |
| 10 | +function enableForm() { |
| 11 | + $('#playlist-button').attr('disabled', false); |
| 12 | +} |
| 13 | + |
| 14 | +// Create a private playlist. |
| 15 | +function createPlaylist() { |
| 16 | + var request = gapi.client.youtube.playlists.insert({ |
| 17 | + part: 'snippet,status', |
| 18 | + resource: { |
| 19 | + snippet: { |
| 20 | + title: 'Test Playlist', |
| 21 | + description: 'A private playlist created with the YouTube API' |
| 22 | + }, |
| 23 | + status: { |
| 24 | + privacyStatus: 'private' |
| 25 | + } |
| 26 | + } |
| 27 | + }); |
| 28 | + request.execute(function(response) { |
| 29 | + var result = response.result; |
| 30 | + if (result) { |
| 31 | + playlistId = result.id; |
| 32 | + $('#playlist-id').val(playlistId); |
| 33 | + $('#playlist-title').html(result.snippet.title); |
| 34 | + $('#playlist-description').html(result.snippet.description); |
| 35 | + } else { |
| 36 | + $('#status').html('Could not create playlist'); |
| 37 | + } |
| 38 | + }); |
| 39 | +} |
| 40 | + |
| 41 | +// Add a video ID specified in the form to the playlist. |
| 42 | +function addVideoToPlaylist() { |
| 43 | + addToPlaylist($('#video-id').val()); |
| 44 | +} |
| 45 | + |
| 46 | +// Add a video to a playlist. The "startPos" and "endPos" values let you |
| 47 | +// start and stop the video at specific times when the video is played as |
| 48 | +// part of the playlist. However, these values are not set in this example. |
| 49 | +function addToPlaylist(id, startPos, endPos) { |
| 50 | + var details = { |
| 51 | + videoId: id, |
| 52 | + kind: 'youtube#video' |
| 53 | + } |
| 54 | + if (startPos != undefined) { |
| 55 | + details['startAt'] = startPos; |
| 56 | + } |
| 57 | + if (endPos != undefined) { |
| 58 | + details['endAt'] = endPos; |
| 59 | + } |
| 60 | + var request = gapi.client.youtube.playlistItems.insert({ |
| 61 | + part: 'snippet', |
| 62 | + resource: { |
| 63 | + snippet: { |
| 64 | + playlistId: playlistId, |
| 65 | + resourceId: details |
| 66 | + } |
| 67 | + } |
| 68 | + }); |
| 69 | + request.execute(function(response) { |
| 70 | + $('#status').html('<pre>' + JSON.stringify(response.result) + '</pre>'); |
| 71 | + }); |
| 72 | +} |
0 commit comments