【问题标题】:Soundcloud HTML5 widget continuous playSoundcloud HTML5 小部件连续播放
【发布时间】:2013-01-03 22:30:22
【问题描述】:

我正在尝试让 SoundCloud HTML5 播放器小部件自动启动并寻找特定的曲目和位置,但无论我尝试什么都不起作用。

我正在使用下面的 API 代码:

<iframe width="100%" height="450" scrolling="no" id="soundcloud-player" frameborder="no" src="https://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Fplaylists%2F3058825&amp;color=00be53&amp;auto_play=false&amp;show_artwork=true"></iframe>
<script type="text/javascript" src="http://w.soundcloud.com/player/api.js"></script>

 <script type="text/javascript">

 (function(){
    var widgetIframe = document.getElementById('soundcloud-player'),
    widget       = SC.Widget(widgetIframe);

    widget.bind(SC.Widget.Events.READY, function() {

       widget.play();
       widget.seekTo('5000');

    });


  widget.bind(SC.Widget.Events.PLAY, function() {        

    // get information about currently playing sound
    widget.getCurrentSound(function(currentSound) {
      console.log('sound ' + currentSound.title + 'began to play');
    });
});  }());

我基本上想要完成的是,当用户在网站上的页面之间切换时,让播放器自动寻找相同的位置。我计划从 cookie 中读取位置和轨迹,然后使用上述方法。任何帮助将不胜感激!

【问题讨论】:

    标签: soundcloud


    【解决方案1】:

    问题很可能与您尝试呼叫seekTo 时声音未完全加载有关。您可以通过在代码中添加以下位来轻松验证这一点:

    // …
    widget.bind(SC.Widget.Events.READY, function() {
      widget.play();
      // Note setTimeout here!
      // This will now work since the needed part of the sound 
      // will have loaded after the timeout
      setTimeout(function () { 
        widget.seekTo('5000'); 
      }, 1000);
    });
    // …
    

    但由于您真的不想在代码中出现任意超时,因此最好将事件处理程序附加到进度事件:

    widget.bind(SC.Widget.Events.LOAD_PROGRESS, function onLoadProgress (e) {
      if (e.loadedProgress && e.loadedProgress === 1) {
        widget.seekTo(15000); // seek to previous location
        widget.unbind(SC.Widget.Events.LOAD_PROGRESS);
      }
    });
    

    这是此代码的工作版本http://jsbin.com/ebeboj/2/edit

    此外,如果您的曲目很长,您还可以从声音中检索duration(通过getCurrentSound),检查曲目在0到1范围内的哪个点停止播放,然后等待值(因为 loadedProgress === 1 可能需要一段时间),类似于:

    widget.getCurrentSound(function(currentSound) {
      // currrentSound.duration is 269896 for the first track of your playlist
      relativePreviousPlay = previousPlay / currentSound.duration; // ~0.204
    });
    
    widget.bind(SC.Widget.Events.LOAD_PROGRESS, function onLoadProgress (e) {
      if (e.loadedProgress && e.loadedProgress > relativePreviousPlay) {
        widget.seekTo(previousPlay); // seek to previous location
        widget.unbind(SC.Widget.Events.LOAD_PROGRESS);
      }
    });    
    

    在此处查看最后一段代码的工作示例http://jsbin.com/ebeboj/4/edit

    旁注:我建议在 cookie 上使用 localStorage 来存储以前的播放位置,因为 cookie 会在客户端和服务器之间来回传输,从而减慢您的网站速度,而您可能不会不需要服务器端的信息。

    【讨论】:

    • 您好,感谢您提供的信息。非常有帮助。 LOAD_PROGRESS 检查成功了。我在这里发布了一个更新的工作版本,它记住了位置、状态和跟踪索引。 [link]jsbin.com/apajug/2我现在还在使用cookies,但我有时间会检查本地存储。我还必须添加一些 hacky widget.play() widget.stop() 来让歌曲开始下载。不确定是否有更好的方法来做到这一点。
    • 不幸的是,hacky play().pause()(请注意,您可以将它们链接起来)似乎是要走的路。
    • LOAD_PROGRESS 的问题在于它只给了我 2 个读数。 0 和 1,中间什么都没有,我只能使用 event.loadProgress 中的 PLAY 事件绑定来获取这些读数。我无法让 LOAD_PROGRESS 在 iOS12 上工作?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-18
    • 1970-01-01
    相关资源
    最近更新 更多