2016-05-05 8 views
0

Dieser Code nimmt die Eingabe einer SoundCloud-Track-URL und lädt sie in das Widget. Gibt es überhaupt die Möglichkeit, das Genre/Tags dieses Liedes mithilfe der SoundCloud API zu extrahieren und anzuzeigen?SoundCloud API - Wie kann man das Genre eines Tracks in das Widget laden?

<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> 

Antwort

0

die Soundcloud widget has a getter method, getCurrentSound(), das wird diese Informationen für Sie zurück!

Beachten Sie, dass Sie getCurrentSound() nur aufrufen möchten, wenn das Widget einen Song geladen hat. Andernfalls wird null zurückgegeben. SC bietet bereits ein fertiges Ereignis, SC.Widget.Events.READY, dass Sie das binden, so dass nach widget.load(input.value); dieses Bit hinzufügen:

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 
    }); 
}); 

Ich habe einen jsfiddle hier, wo ich die getCurrentSound brach in ein zurufen separate Funktion namens showSongInfo: https://jsfiddle.net/dfnny3rp/

+0

Sie sind ein Lebensretter! Danke vielmals! – mellows