编程语言
首页 > 编程语言> > javascript – SoundCloud SDK / API回调事件未触发

javascript – SoundCloud SDK / API回调事件未触发

作者:互联网

这是一个我认为应该按照文档工作的简单示例.流播放,但回调不会触发.我犯了一个错误,或者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',
          {
              onl oad: 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>

这是一个类似的问题,但没有答案. Soundcloud Javascript SDK 2.0 – Stream onfinish event does not execute

解决方法:

使用html5时,您可以直接定位音频元素,并将自定义操作绑定到回调事件上.以下示例大致相当于未触发的SDK事件.这是一种解决方法,但由于html5是默认方法,因此它对于包括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>

以下是更多可用的html5媒体事件列表:
https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Media_events

标签:soundcloud,javascript,api,sdk
来源: https://codeday.me/bug/20190824/1709497.html