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?
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'); });
});
This works perfectly with the SDK Version 3
SC.stream('/tracks/293', function(player) {
player.on('finish', function() {
stopPlayer();
});
});
I have the same issue, I can't seem to get any of the callbacks to fire. Here's a trivial example, working aside from callbacks. (This should be a comment rather than an answer, but I don't have the SO reputation to do so.)
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>Sound Cloud API callback test</h1>
<script src="//connect.soundcloud.com/sdk-2.0.0.js"></script>
<script type="text/javascript">
SC.initialize({
client_id: "YOUR_CLIENT_ID"
});
SC.stream(
'/tracks/293',
{
onload: function() { console.log("Does not fire."); },
onplay: function() { console.log("Does not fire."); },
onfinish: function() { console.log("Does not fire."); }
},
function(sound) { sound.play(); });
</script>
</body>
</html>