I am using the default SoundCloud audio widget to play a playlist and would like the songs to play in a random order. I am using the SoundCloud JavaScript SDK oEmbed object (the iframe version).
<script src="//connect.soundcloud.com/sdk.js"></script>
SC.oEmbed("//soundcloud.com/buzzinfly", {
auto_play: true,
start_track: random_start_track,
iframe: true,
maxwidth: 480,
enable_api: true,
randomize: true
},
document.getElementById("soundcloud_player"));
The documentation doesn't mention any randomize or shuffle attribute, but other undocumented attributes like start_track are supported with oembed.
//developers.soundcloud.com/docs/oembed#parameters
//developers.soundcloud.com/docs/widget
Does any one know if there is an attribute to shuffle the song order in the default audio widget?
If not, is there is a way to capture events from the default SoundCloud widget, so I can play a random song after a song finish event? Like you can with the custom player.
//developers.soundcloud.com/docs/custom-player#events
$(document).bind('onPlayerTrackSwitch.scPlayer', function(event, track){
// goto random track
});
Thanks
Update
Here is code am I using after getting gryzzly's help.
The next button works great. I can skip thru random songs. Unfortunately, when a track finishes playing, I hear the audio of two songs: the next song in the playlist and the random track that is skipped to in the FINISH event.
var widget = null;
var song_indexes = new Array();
var current_index = 0;
$(function() {
var iframe = document.querySelector('#soundcloud_player iframe');
widget = SC.Widget(iframe);
widget.bind(SC.Widget.Events.READY, function() {
widget.unbind(SC.Widget.Events.FINISH);
widget.bind(SC.Widget.Events.FINISH, function() {
play_next_shuffled_song();
});
widget.getSounds(function(sounds) {
create_shuffled_indexes(sounds.length);
play_next_shuffled_song();
});
});
$("#button_sc_next").on("click", play_next_shuffled_song);
});
function play_next_shuffled_song() {
current_index++;
if (current_index >= song_indexes.length) {
current_index = 0;
}
var track_number = song_indexes[current_index];
widget.skip(track_number);
}
function create_shuffled_indexes (num_songs) {
for (var i=0;i<num_songs;i++) {
song_indexes.push(i);
}
song_indexes = shuffle(song_indexes);
}
//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/array/shuffle [v1.0]
function shuffle(o){ //v1.0
for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
};
You can find the documentation for Widget API here.
Pseudo code for this would be something like:
load widget
get widget sounds LENGTH /* say 5 */
create an ARRAY of that LENGTH and shuffle it /* to have something like [3, 1, 0, 2, 4] */
create function SKIP that will check if ARRAY still has any items in it (length is not 0) and skip player to array.pop() index /* pop will return the last item in the array and return its value */
attach event handler to FINISH events that will run SKIP function
run SKIP function first time yourself
If user skips with the “next” control in widget's UI the widget will just play next song in the list. You could perhaps detect that somehow and skip to another random track.
I hope this helps!