Soundcloud Javascript SDK 2.0 - Stream onfinish event does not execute

前端 未结 3 1038
梦如初夏
梦如初夏 2020-12-21 00:25

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. A

相关标签:
3条回答
  • 2020-12-21 01:02

    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>
    
    0 讨论(0)
  • 2020-12-21 01:04

    This works perfectly with the SDK Version 3

    SC.stream('/tracks/293', function(player) {
      player.on('finish', function() {
        stopPlayer();
      });
    });
    
    0 讨论(0)
  • 2020-12-21 01:28

    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'); });
    });
    
    0 讨论(0)
提交回复
热议问题