Soundcloud Javascript SDK 2.0 - Stream onfinish event does not execute

霸气de小男生 提交于 2019-11-29 14:43:51

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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!