这是一个简单的例子,我认为应该按照文档工作。流播放,但回调不触发。是我弄错了,还是 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 event does not execute
最佳答案
使用 html5 时,您可以直接将音频元素作为目标,并将自定义操作绑定(bind)到该元素上的回调事件。下面的示例大致相当于未触发的 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
关于javascript - SoundCloud SDK/API 回调事件未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31143925/