SoundCloud SDK/API 回调事件未触发 [英] SoundCloud SDK / API callback events not firing

查看:33
本文介绍了SoundCloud SDK/API 回调事件未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个简单的例子,我认为应该按照文档工作.流播放,但回调不触发.是我弄错了,还是 SDK/API/SoundManager 之间存在错误?

Here's a simple example that I believe should be working per the docs. The stream plays, but the callbacks don't fire. Am I making a mistake, or is there a bug somewhere between the 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>

这是一个类似的问题,但没有答案.Soundcloud Javascript SDK 2.0 - Stream onfinish 事件不执行

This is a similar question, but there have been no answers. Soundcloud Javascript SDK 2.0 - Stream onfinish event does not execute

推荐答案

使用 html5 时,您可以直接定位音频元素并将自定义操作绑定到该元素上的回调事件.下面的示例大致相当于未触发的 SDK 事件.这是一种解决方法,但由于 html5 是默认方法,因此对于包括 iOS 在内的常见使用来说是可靠的.

When using html5, you can target the audio element directly and bind custom actions to the callback events on that instead. The examples below are roughly equivalent to your SDK events that aren't firing. It's a workaround, but since html5 is the default method, it's reliable for common 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>

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

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

这篇关于SoundCloud SDK/API 回调事件未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆