SoundCloud SDK / API sound events not firing

Here's a simple example that I believe should work for docs. The thread is playing, but the callbacks are not firing. Am I wrong, or is there a bug somewhere between SDK / API / SoundManager?

<!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>

      

This is a similar question, but there were no answers. Soundcloud Javascript SDK 2.0 - onfinish event for stream not firing

+3


source to share


2 answers


When using html5 you can target the audio element directly and bind your own actions to callback events on that. The examples below are roughly equivalent to your SDK events not firing. This is a workaround, but since html5 is the default method, it is reliable for general use, including iOS.

<!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',
            function(sound) {
                html5Audio = sound._player._html5Audio;
                html5Audio.addEventListener('canplay', function(){ console.log('event fired: canplay'); });
                html5Audio.addEventListener('play',    function(){ console.log('event fired: play'); });
                html5Audio.addEventListener('ended',   function(){ console.log('event fired: ended'); });
                sound.play();
            });
</script>
</body>
</html>

      



Here is a list of more html5 media events available:
https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Media_events

+4


source


I can't leave a comment, so treat it as one. For unknown reasons (at least to me) what you actually get as an audio object in SDK 2 is a player, not a track. It works correctly if you use sdk.js instead.



+2


source







All Articles