【问题标题】:Soundcloud: How do I know the sound cloud track ID right away after uploading a track?Soundcloud:如何在上传曲目后立即知道声音云曲目 ID?
【发布时间】:2013-09-26 07:25:42
【问题描述】:

http://developers.soundcloud.com/docs/api

当我查看 API 文档时,我看到了

 SC.stream("/tracks/293", function(sound){
      sound.play();
 });

当我查看我上传的曲目时,它只为我提供了永久链接。如何从网站获取曲目 ID?我是否总是必须执行 /resolve 才能获取 ID?

【问题讨论】:

    标签: soundcloud


    【解决方案1】:

    这可能是比 /resolve 更多的手动步骤,但它是“来自网站”。当您转到声音并单击“共享”时,声音 id 也会出现在嵌入代码中。

    例如,如果你去一个声音页面,例如:

    https://soundcloud.com/lowcountrykingdom/another-ordinary-day

    然后点击“分享”,弹出一个窗口。点击“嵌入”切换到嵌入选项卡,复制嵌入代码,如下所示:

    <iframe width="100%" height="166" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/47580057&amp;color=ff6600&amp;auto_play=false&amp;show_artwork=true"></iframe>
    

    注意url查询参数值中的ID:

    url=https%3A//api.soundcloud.com/tracks/47580057
    

    【讨论】:

    • 这似乎不再起作用。我现在只在分享链接中看到纯文本版本的名称。
    • 如何以编程方式获取?
    • 如果您使用的是客户端 SC 库,看起来现在提供了 ID,而它们以前没有提供
    【解决方案2】:

    我会使用 jquery ajax 调用从 Soundcloud 中获取数据。假设您保存变量 permalink_url 和 client_id:

    $.get('http://api.soundcloud.com/resolve.json?url='+
        permalink_url+'/tracks&client_id='+client_id , function (result) {
            console.log(result);
        });
    

    这应该记录一组歌曲。查看此垃圾箱以供参考http://jsbin.com/viviza/4/edit

    更新:这个答案已经很老了。

    Soundclick 文档https://developers.soundcloud.com/docs/api#uploading

    SC 对象现在允许直接获取 id

    SC.connect().then(function(){
        return SC.get('/me/tracks');
    }).then(function(tracks){
        return tracks.map(function(track){
           console.log("you can log the id here too: " + track.id")
           return track.id
        })
    })
    

    【讨论】:

      【解决方案3】:

      您还可以从给定的用户 ID 中获取所有曲目。然后使用 $.map() 将每个轨道放入一个数组中。使用 song[i].id 调用 SC.stream() 以从曲目数组的数组中播放一首随机歌曲。

      SC.get('/users/123456/tracks/', function(tracks) {
      
        // get an array of tracks
        var song = $.map(tracks, function(i){
          return i;
        });
      
        var i = Math.floor(Math.random() * song.length)  // get a random value between 0 & the  of songs in SC account -1
      
      
        SC.stream(song[i].id, function(sound){
          sound.play();
        });
      });
      

      【讨论】:

        【解决方案4】:

        使用 JavaScript 从 soundcloud 歌曲 url 中查找曲目数据:

        let trackUrl = 'https://soundcloud.com/user-869590724/talk-to-animals-sentra-remix'
        let client_id = '<your-client-id>'
        
        let resolveUrl = `http://api.soundcloud.com/resolve.json?url=${trackUrl}/tracks&client_id=${client_id}`
        fetch(resolveUrl, {
          method: 'get'
        }).then((response) => {
          return response.json()
        }).then((result) => {
          /* now you have the track */
          console.log(result)
        })
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-03-21
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多