How to play a playlist using only SoundCloud's JavaScript Streaming SDK


#1

Ok, so I am very confused on how to use SoundCloud's API. Basically, I want to automatically play a public playlist, but with the ability to pause, skip and mute.

So far I've been able to get the track id for all of the tracks on my playlist using this code:

   var i1 = 0; 
    var currentSong = 0; //song index
    var currentMusicState = 'play';
    var song = { 
        title: [],
        artist: [],
        id: []
    };
    SC.initialize({
            client_id: 'my client id'
        });
        SC.get('/playlists/234357794').then(function (playlist) {
            playlist.tracks.forEach(function (track) {
                song.id[i1] = track.id;
                song.title[i1] = track.title;
                song.artist[i1] = track.user.username;
                i1++;
            });
        });

That seems to work for what I've tried, but here is where I get incredibly confused.
How am I supposed to stream the music and control it.
I saw SoundCloud's documentation and it is incredibly simplistic with little detail (or maybe I'm just too much of a newbie).
With this code, it successfully streams one song, but fails to do anything else. Can anybody help me?

SC.stream(('/tracks/' + song.id[currentSong])).then(function (musicPlayer) {
                musicPlayer.play();
                currentMusicState = 'play'; 
                if (currentSong < song.id.length) {
                    currentSong++; //next index for next song id
                }
                else{
                    currentSong = 0; //else loop back to first song
                }
                musicPlayer.on('finish', function(){ //this does nothing, but I'm pretty sure I'm during this wrong
                    playMusic(); //recursive function call
                });
            });

#2

Not sure if you still are looking for help, seeing as this is a year old, but I just got started using the API and was struggling as well. I finally managed to get it streaming a full playlist. I did eliminate the pause/skip feature, but that should be fairly easy to implement. I used two functions that call each other and a setTimeout to play more than one song. In the documentation, you can also get a songs state. with "finished" being sent if the song is over. You might be able to also use that.

SC.initialize({
    client_id: 'YOUR_CLIENT_ID'
});

//Main Index
var index = 0;
//Index for current song
var currentSong = 0;
//Song data
var song = {
    title: [],
    artist: [],
    id: [],
    duration: []
};

//Get one of my playlists and store the data of each song
SC.get('/playlists/295366134').then(function(playlist) {
    playlist.tracks.forEach(function (track) {
        song.id[index] = track.id;
        console.log(track.title);
        song.title[index] = track.title;
        song.artist[index] = track.user.username;
        song.duration[index] = track.duration;
        console.log(track.duration);
        index++;
    });

    playCurrentSong();

    function playCurrentSong() {
        console.log("got into playCurrentSong function");
        //Stream playlist, looping when end of playlist is reached
        SC.stream('/tracks/' + song.id[currentSong]).then(function (player) {
            player.play();
        });
        console.log("duration " + song.duration[currentSong]);
        setTimeout(queueNextSong, song.duration[currentSong]);
    }

    function queueNextSong() {
        console.log("got into queueNextSong function");

        if (currentSong < song.id.length) {
            console.log(currentSong);
            //next index for next song id
            currentSong++;
        }
        else {
            currentSong = 0;
            console.log(currentSong)
        }
        playCurrentSong();
    }
});