2013-04-25 61 views
0

首先,我是一個小菜鳥。SoundCloud嵌入式軌道:如何在軌道結束播放後刷新頁面?

好的,所以我在我的網頁中嵌入了一個SoundCloud曲目。我的問題是,如何在賽道結束時刷新頁面(或做其他事情)?

你可以用getDuration來做到嗎(我發現在SoundCloud API頁面上)?

我試着編碼它。在代碼中,我嘗試獲取曲目的持續時間,然後將其打印在屏幕/網頁上。這段代碼有什麼問題?

<script src="https://w.soundcloud.com/player/api.js" type="text/javascript"></script> 
<span id="headerLeft-content"> 
    <script type="text/javascript"> 
     var duration = 0; 
     (function(){ 
       var widgetIframe = document.getElementById('sc-widget'), 
       widget = SC.Widget(widgetIframe); 
       widget.bind(SC.Widget.Events.READY, function() { 
       widget.getDuration(function(val) { 
        duration = val; 
       }); 
       }); 
     }()); 

     document.write(duration); 
    </script> 
</span> 

是否奏效,我只想把像等待(持續時間),然後刷新...

換句話說,可嵌入的SoundCloud軌道被「黑客」循環(或刷新頁面在軌道結束之後,這就是我想要做的),即使原始小部件不支持循環?

請隨便看看的SoundCloud HTML5控件頁面,我發現getDuration命令,看看你能不能幫我... =>http://developers.soundcloud.com/docs/api/html5-widget#getters

編輯:

<script> 
var html=<iframe blablabla...></iframe> 
document.write(html); 
</script> 

<script src="https://w.soundcloud.com/player/api.js" type="text/javascript"></script> 
<script type="text/javascript"> 
    (function(){ 
    var widgetIframe = document.getElementById('sc-widget'), 
     widget  = SC.Widget(widgetIframe), 
    widget.bind(SC.Widget.FINISH, function() { 
      window.location.reload(false); 
    }); 
    }()); 
</script> 

頁不賽道結束後刷新。你能看到有什麼問題嗎?

回答

1

您可以將函數綁定到SC.Widget.FINISH事件documented here。下面的代碼片段應該工作:

widget.bind(SC.Widget.FINISH, function() { 
    window.location.reload(false); 
}); 

當然,如果你想要的是用於微循環,您可以使用seekToplay方法:

widget.bind(SC.Widget.FINISH, function() { 
    // again, again! 
    widget.seekTo(0); 
    widget.play(); 
}); 

這將是比少侵擾頁面刷新。

+0

感謝您的回覆!我的代碼現在看起來像這樣,但它似乎並沒有工作(看文章中的編輯部分) – guestsc2013 2013-04-26 05:17:19

0
widget.bind(SC.Widget.Event.FINISH, function() { 
    widget.seekTo(0); 
    widget.play(); 
}); 

在另一個回覆中,第一個參數缺少「.Event」。

0

最後。一個真正的工作版本。

var widgetIframe = document.getElementById("soundcloud"), 
    widget  = SC.Widget(widgetIframe); 

widget.bind(SC.Widget.Events.FINISH, function() { 
    widget.play(); 
});