Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Soundcloud Javascript SDK 2.0 - Stream onfinish event does not execute

I'm trying to use the onfinish event for the Soundcloud Javascript SDK Stream method. I have the following code that is working except for the onfinish event never fires. Any ideas would be very helpful.

var soundPlayer;
var smOptions = {
    useHTML5Audio: true,
    preferFlash: false,
    onfinish: function() {
        console.log('Finished playing');
    }
};

SC.initialize({
    client_id: "..."
});

SC.stream("/tracks/12345", smOptions, function(sound) {
    soundPlayer = sound;
});

Thanks in advance!

Update:

Since onfinish does not work I am using the getState() method on the soundPlayer object that is returned in SC.stream(). Thankfully there is a unique state called "ended" when the stream has completed. This is what I have implemented:

setInterval(function() {
    if (soundPlayer != null && soundPlayer.getState() == "ended") {
        soundPlayer.play();
    }
}, 250);

I'm not happy about it, but it gives me the experienced desired and meets the requirements. I really hope someone can help shed some light on this on why the documented onfinish method is not firing.

Anyone have any ideas?

like image 207
Mike Avatar asked Oct 20 '22 08:10

Mike


1 Answers

Here's a workaround with callbacks instead of polling. It works by targeting the dynamic html5 audio player element itself & registering a native event. I don't expect it to work in fallback modes, but forcing html5 may finish up the solution until the SDK v2 returns a sound object.

var soundPlayer;
var smOptions = {
    useHTML5Audio: true,
    preferFlash: false
};

SC.initialize({
    client_id: "..."
});

SC.stream("/tracks/12345", smOptions, function(sound) {
    soundPlayer = sound;
    html5Audio = sound._player._html5Audio;
    html5Audio.addEventListener('ended', function(){ console.log('event fired: ended'); });
});
like image 194
Craig Lyons Avatar answered Oct 22 '22 00:10

Craig Lyons