Dirty Secretz

Icon

Official site of DJ/Producers, Dirty Secretz

Soundcloud random play

Picture 5 We love our Soundcloud player (a normal, uncreepy kinda love). The only problem we’ve had is that it plays the playlist in the same order each time you load our site and we don’t want you getting bored of the same tune now do we? Well we might just have the answer…

After doing a bit of reading of all the functions in Soundcloud we ran across some information on the commands possible here. So after a bit of fumbling around with some of the examples, we managed to come up with a few lines of javascript that seem to do the job!

noTracks = player.api_getTracks();
randomTrack = Math.floor(Math.random()*noTracks.length);
player.api_skip(randomTrack);
player.api_play();

Basically what is does is get the amount of tracks in the playlist, pick a random one from it then play it. Simples! Here’s the full code… (we won’t bother explaining everything else, it’s all here if you need any help)

<script type='text/javascript' src='http://www.dirty-secretz.com/wp-includes/js/soundcloud.player.api.js'></script>
  <script type="text/javascript" charset="utf-8">
    // ------- a simple auto-play example ----------
      $(document).bind('soundcloud:onPlayerReady', function(event, data) {
        var apiUrl = data.mediaUri + '.json?callback=?',
            player = event.target;
            noTracks = player.api_getTracks();
            randomTrack = Math.floor(Math.random()*noTracks.length);
            player.api_skip(randomTrack);
            player.api_play();
      });
  </script>

Category: General

Tagged:

Leave a Reply

You must be logged in to post a comment.