阅读背景:

SoundCloud API - 任何提取加载到窗口小部件的轨道类型的方法?

来源:互联网 

This code takes input of a SoundCloud track URL and loads it in to the widget. Is there anyway of being able to extract and display the genre/tags of this song using the SoundCloud API?

此代码接收SoundCloud轨道URL的输入并将其加载到窗口小部件中。有没有能够使用SoundCloud API提取和显示这首歌的流派/标签?

<html>
<head>


    <script type='text/javascript' src='https://w.soundcloud.com/player/api.js'></script>  


</head>
<body>
    <div id="sectionLoad">

        <input type="text" id="url" value=""><br>
        <div id="gap"></div>
        <a id="loadButton" onclick="loadSong();" class="button">LOAD TRACK</a>
        <div id="gap2"></div>

        <iframe id = "sc-widget" width="80%" height="80%" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=single_active=false" ></iframe>

    </div>
<script>
function loadSong(){
    (function() {

        var iframe = document.querySelector('#sc-widget');
        var widget = SC.Widget(iframe);
        var input = document.getElementById("url");

        widget.load(input.value); 
        console.log(widget);   

    }());
}
</script>

</body>
</html>

1 个解决方案

#1


0  

the Soundcloud widget has a getter method, getCurrentSound(), that'll return that information for you!

Soundcloud小部件有一个getter方法getCurrentSound(),它将为您返回该信息!

Notice you'll want to call getCurrentSound() only when the widget is done loading a song, otherwise it'll return null. SC already provides a ready event, SC.Widget.Events.READY, that you can bind that to, so after widget.load(input.value); add this bit:

请注意,只有在窗口小部件加载完歌曲时才会调用getCurrentSound(),否则它将返回null。 SC已经提供了一个就绪事件SC.Widget.Events.READY,你可以将它绑定到,所以在widget.load(input.value)之后;添加这个位:

widget.bind(SC.Widget.Events.READY, function(){
    widget.getCurrentSound(function(currentSound) {
    console.log(currentSound);
    var genre = currentSound.genre;
    var tags = currentSound.tag_list;
    // add genre and tags to appropriate elements
  });
});

I made a jsfiddle here where I broke the getCurrentSound call out into a separate function called showSongInfo: https://jsfiddle.net/dfnny3rp/

我在这里创建了一个jsfiddle,我将getCurrentSound调用打包成一个名为showSongInfo的独立函数:https://jsfiddle.net/dfnny3rp/


分享到: