如何使用SoundCloud API按类型过滤

时间:2015-03-01 12:55:32

标签: javascript html soundcloud

我正在尝试使用SoundCloud的API但没有成功,网上的大多数示例和教程都无法正常工作。 出于此目的,我使用了这个video tutorial。我希望在我的网站上创建一个类型过滤器。 所以首先我创建了一个HTML:

<!DOCTYPE html>
<html>
    <head>
        <script src="//connect.soundcloud.com/sdk.js"></script>
        <script src="js/soundcloud.js"></script>
    </head>
    <body>
        <div id="target">
            <ul>
                <li><a href="#" class="genre">punk</a></li>
                <li><a href="#" class="genre">rap</a></li>
                <li><a href="#" class="genre">rock</a></li>
            </ul>
        </div>
    </body>
</html>

而不是JavaScript(soundcloud.js):

function playSomeSound(genre){
SC.get('/tracks',{
    genres:genre,
    bpm:{
      from:100
    }
  }, function(tracks){
    var random=Math.floor(Math.random()*49);
    SC.oEmbed(tracks[random]).uri,{auto_play:true}, document.getElementById('target')
  });
}

window.onload=function(){
  SC.initialize({
    client_id: 'my_app_id'
  });

  var menuLinks=document.getElementsByClassName('genre');
  for (var i=0; i<menuLinks.lenght;i++){
    var menuLink=menuLinks[i];
    menuLink.onclick=function(e){
      e.preventDefault();
      playSomeSound(menuLink.innerHTML);
    }
  }

};

当我导航到我的网站时,一切都很好,我从控制台得到没有错误,但是如果我点击一个类型,它什么都不做。 为什么它不能从SoundCloud中检索歌曲? SoundCloud为API使用改变了不同的东西,还有另一种方法吗?

1 个答案:

答案 0 :(得分:1)

for (var i=0; i<menuLinks.lenght;i++){ //it should be .length and not lenght
    var menuLink=menuLinks[i];
    menuLink.onclick=function(e){
      e.preventDefault();
      playSomeSound(menuLink.innerHTML);
    }
  }

另外,你错放了SC.embed上的括号。这是一个有效的解决方案:http://jsbin.com/pugegesepu/1/edit