此代码接收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>
答案 0 :(得分:0)
Soundcloud widget has a getter method,getCurrentSound(),它将为您返回该信息!
请注意,您只想在小部件加载完歌曲时调用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
});
});
我在这里创建了一个jsfiddle,我把getCurrentSound调用打破了一个名为showSongInfo的独立函数:https://jsfiddle.net/dfnny3rp/