Soundcloud Javascript SDK 2.0 - 流onfinish事件不会执行 [英] Soundcloud Javascript SDK 2.0 - Stream onfinish event does not execute
问题描述
我正在尝试将onfinish事件用于Soundcloud Javascript SDK Stream方法。我有以下代码正在工作,除了onfinish事件永远不会触发。任何想法都会非常有用。
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. Any ideas would be very helpful.
var soundPlayer;
var smOptions = {
useHTML5Audio: true,
preferFlash: false,
onfinish: function() {
console.log('Finished playing');
}
};
SC.initialize({
client_id: "..."
});
SC.stream("/tracks/12345", smOptions, function(sound) {
soundPlayer = sound;
});
提前致谢!
更新:
由于onfinish不起作用,我在SC.stream()中返回的soundPlayer对象上使用getState()方法。值得庆幸的是,当流完成时,有一个名为已结束的唯一状态。这就是我实现的:
Since onfinish does not work I am using the getState() method on the soundPlayer object that is returned in SC.stream(). Thankfully there is a unique state called "ended" when the stream has completed. This is what I have implemented:
setInterval(function() {
if (soundPlayer != null && soundPlayer.getState() == "ended") {
soundPlayer.play();
}
}, 250);
我对它并不满意,但它给了我经验丰富的所需并满足要求。我真的希望有人可以帮助解释为什么记录的onfinish方法没有解雇。
I'm not happy about it, but it gives me the experienced desired and meets the requirements. I really hope someone can help shed some light on this on why the documented onfinish method is not firing.
任何人都有任何想法?
推荐答案
这是一个使用回调而不是轮询的解决方法。它的工作原理是瞄准动态html5音频播放器元素本身&注册本机事件。我不希望它在后备模式下工作,但强制html5可能会完成解决方案,直到SDK v2返回声音对象。
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'); });
});
这篇关于Soundcloud Javascript SDK 2.0 - 流onfinish事件不会执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!