简体   繁体   English

如何使用 Soundcloud API 获取播放列表的每个曲目?

[英]How to get each track of a playlist with the Soundcloud API?

I am working with the Soundcloud API to create a custom playlist (another look than the original Soundcloud player).我正在使用 Soundcloud API 创建自定义播放列表(不同于原始 Soundcloud 播放器的外观)。 See image below.见下图。 I am develop a website for an artist.我正在为一位艺术家开发一个网站。

With the API of Soundcloud I want to import his music from Soundcloud to the website.使用 Soundcloud 的 API,我想将他的音乐从 Soundcloud 导入网站。 There are different tabs that should represents all albums - you can see this on the picture - that are uploaded in the SC database.有不同的选项卡应该代表所有专辑——你可以在图片上看到这一点——它们被上传到 SC 数据库中。 This I have done with the following code:这是我用以下代码完成的:

SC.get('/users/' + USER + '/playlists', function(playlists) {
    $(playlists).each(function(index, playlist) {
    $('h2').html(playlist.title);

    $('.list-of-links').append($('<li></li>').html('<a href="#">' + playlist.title + '</a>'));
//$('#cover').append($('<li></li>').css('background-image', "url('" + track.artwork_url + "')");
    $("#cover").append('<div><img src="' + playlist.artwork_url.replace('-large', '-t500x500') + '"></div>');

    });
    });

Where USER is a global variable where the name "mo-rooneh" is stored in.其中USER是一个全局变量,其中存储了名称“mo-rooneh”。

在此处输入图片说明

So that's working correct.所以这是正确的。 Now I want to get from each album, (1) the album title and (2) all the tracks of that album, that contains a start and stop button to play each track and the amount of likes listed in a UL list.现在我想从每张专辑中获取(1)专辑名称和(2)该专辑的所有曲目,其中包含一个开始和停止按钮来播放每首曲目以及UL列表中列出的喜欢数量。 I don't know how to achieve this.我不知道如何实现这一目标。 What I have now is that the code returns the album title and all tracks of the user.我现在拥有的是代码返回专辑标题和用户的所有曲目。 Know it posts for each album all tracks of the user, so also tracks from other albums.知道它会为每个专辑发布用户的所有曲目,其他专辑中的曲目也是如此。 What I want is that it will show me the tracks of that specific album:我想要的是它会向我显示该特定专辑的曲目:

SC.connect(function() {

        SC.get('/users/' + USER + '/playlists', function(playlists) {

            $(playlists).each(function(index, playlist) {

                SC.get('/users/' + USER + '/tracks', function(tracks) {

                    $('.test').append($('<h2></h2>').html('<a href="#">' + playlist.title + '</a>'));

                    $(tracks).each(function(index, track) {
                        $('.test').append($('<p></p>').html('<a href="#">' + track.title + '</a>'));

                    });

                });


            });

        });
    });

I look out for your answer.我期待你的回答。 Thanks in advance.提前致谢。

Soundcloud's developer docs state that each playlist contains an array of tracks (see https://developers.soundcloud.com/docs/api/reference#playlists ). Soundcloud 的开发人员文档指出,每个播放列表都包含一系列曲目(请参阅https://developers.soundcloud.com/docs/api/reference#playlists )。

You should be able to loop through the array and populate the html with the relevant values by effectively joining the code in your question together, though you will want to either use classes in the markup or generate individual IDs for each playlist (maybe something like "playlist"+playlist.id ).您应该能够通过将问题中的代码有效地连接在一起来遍历数组并使用相关值填充 html,尽管您可能希望在标记中使用类或为每个播放列表生成单独的 ID(可能类似于"playlist"+playlist.id )。

FURTHER EDIT : Updated to include a containing object to access methods for each track by ID reference.进一步编辑:更新为包含一个包含对象以通过 ID 引用访问每个轨道的方法。

var track_objects = {};

SC.get('/users/' + USER + '/playlists', function(playlists) {

  $(playlists).each(function(index, playlist) {

    // Let's say you have divs with class 'playlist', adhering to the structure of the markup you imply in your question.
    // Store reference to the current playlist
    var $playlist = $('.playlist').eq(index);
    // Populate with values
    $playlist.find('h2').html(playlist.title);
    $playlist.find('.list-of-links').append($('<li></li>').html('<a href="#">' + playlist.title + '</a>'));
    $playlist.find('.cover').append('<div><img src="' + playlist.artwork_url.replace('-large', '-t500x500') + '"></div>');

    // Maybe you would have a ul in each of your playlist divs with a class of tracks...
    // Store reference to the track ul
    var $tracks = $playlist.find('ul.tracks');

    $(playlist.tracks).each(function(i, track) { // Use 'i' so there's no conflict with 'index' of parent loop

      // Your count var is unnecessary, i is 0-indexed so just add 1 to get the track number
      // Note use of track.id here, and different classes for start / stop
      $tracks.append($('<li data-id="' + track.id + '"></li>').html('<a href="#" class="start">Start</a> / <a href="#" class="stop">Stop</a>' + (i + 1) + ' / ' + track.title + ' - ' + track.playback_count));

    });

  });

});

EDIT:编辑:

Glad it's working for you (I've been working in vanilla JS for a while so my jQuery is a little rusty...)很高兴它对你有用(我已经在 vanilla JS 工作了一段时间,所以我的 jQuery 有点生疏......)

To stream a sound, all you need to do is assign an event listener to the <a/> element, grab the track id from the data-attribute of the parent node, and call SC.stream with the id variable - see example below (also see https://developers.soundcloud.com/docs/api/guide#streaming ).要流式传输声音,您需要做的就是为<a/>元素分配一个事件侦听器,从父节点的data-attribute中获取轨道 ID,并使用 id 变量调用SC.stream - 请参见下面的示例(另请参阅https://developers.soundcloud.com/docs/api/guide#streaming )。

( Note: changes are required to the code above - use track.id in the data-attribute as Soundcloud's API works with the id rather than the title, and you'll want appropriate classes for start/stop buttons). 注意:需要对上面的代码进行更改 - 在 data-attribute 中使用 track.id 因为 Soundcloud 的 API 使用 id 而不是标题,并且您需要适当的类用于开始/停止按钮)。

EDITED THRICE : Check to see if the sound object has been stored already - if so, simply call play() on the stored object without needing to reinstantiate the Soundcloud streaming object.编辑三次:检查声音对象是否已经存储 - 如果是,只需在存储的对象上调用play() ,而无需重新实例化 Soundcloud 流对象。

// Depending on jQuery version, you may need 'live()' here
$('a.start').on('click', function(){

  var track_id = $(this).parent().attr('data-id');

  if( typeof track_objects[track_id] !== 'undefined' ){
    // We already have this one, no need to make another call to SC.stream
    var sound = track_objects[track_id];
    sound.play();
  }else{
    // First play requested - we need to load this one
    SC.stream('/tracks/' + track_id, function(sound){
      sound.play();
      // Store sound object
      track_objects[track_id] = sound;
    });
  }

});
// Depending on jQuery version, you may need 'live()' here
$('a.stop').on('click', function(){

  var track_id = $(this).parent().attr('data-id');

  // In case a user clicks stop accidentally on a track that hasn't been played yet, check for undefined here too
  if( typeof track_objects[track_id] !== 'undefined' ){
    var sound = track_objects[track_id];
    sound.stop();
  }

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM