【问题标题】:Generating YouTube Custom PlayList issue生成 YouTube 自定义播放列表问题
【发布时间】:2017-08-08 08:44:55
【问题描述】:

我想使用 YouTube API 从this tutorial 构建一个自定义 youtube 播放列表,但我在某个点卡住了。 我基本上嵌入了 client.js 脚本并在加载时执行它的功能,然后我还嵌入了 YouTubePlayList.js 文件,如教程中所述。 这是我正在尝试做的事情。我确实在控制台中收到了 YouTubePlayList 对象,但它似乎没有提供任何适当的数据。我需要一个工作脚本示例或有关如何实现它的指导,并在我的客户端中呈现播放列表。在此先感谢,任何帮助表示赞赏!

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>

【问题讨论】:

    标签: javascript jquery youtube-api


    【解决方案1】:

    您可以访问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 函数来控制视频 播放器以及可用于进一步的播放器参数 自定义播放器。

    【讨论】:

    • 非常感谢您的回答,它确实帮助我找到了另一个很棒的视频资源,我发现是this one ...在这里您还可以了解您实际需要什么请求发送、如何发送以及如何获取需要嵌入到您的个人网页中的数据对象
    猜你喜欢
    • 2019-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多