【问题标题】:How to pause streaming in soundcloud javascript api如何在soundcloud javascript api中暂停流式传输
【发布时间】:2013-02-28 17:50:19
【问题描述】:

我想允许声音文件播放和暂停。该文档显示了如何播放流文件:

    $("#stream").live("click", function(){      
          SC.stream("/tracks/{'track-id'}", function(sound){
             sound.play();
          };

它使用声音管理器进行流式传输并使用它的一些对象和方法......比如 .pause()!

所以这是我认为应该可以工作的代码:

    var is_playing="false";

    $("#stream").live("click", function(){

        SC.stream("/tracks/{'track-id'}", function(sound){
            if (is_playing==="true")
                {
                    sound.pause();
                    is_playing = "false";
                    console.log(is_playing);
                }
            else if (is_playing === "false")
                {
                    sound.play();
                    is_playing = "true";
                    console.log(is_playing);
                }

            });

但是,它会继续播放曲目而不会暂停。有任何想法吗? 谢谢。

【问题讨论】:

  • var is_playing="false".. 你应该使用布尔值。
  • 是的。但是,这并没有解决问题。

标签: javascript jquery api soundcloud soundmanager2


【解决方案1】:
var is_playing = false;
soundManager.setup({
    url: '/swf/soundmanager2.swf',
    preferFlash: false,
    onready: function() {
        var mySound = soundManager.createSound({
            id: 'aSound',
            url: 'sonidos/sonido1.mp3' ,
            autoplay: false
        });

        $("#stream").live("click", function(){    
            if (is_playing==false){
                    mySound.play();
                    is_playing = true;
                }else if (is_playing== true) {
                    mySound.stop();
                    is_playing = false;
             }
        });
    },
    /*    ontimeout: function() {
    // Hrmm, SM2 could not start. Missing SWF? Flash blocked? Show an error, etc.?
    }*/
});
</script>

【讨论】:

【解决方案2】:

你必须使用sound.stop(); 还有一个内置功能可以切换声音的播放/暂停。 使用sound.togglePause(); 来做到这一点。

【讨论】:

    【解决方案3】:
    SC.stream("/tracks/13680213", function(sound) {
        SC.sound = sound;
    });
    

    这意味着您没有在 SC.stream 构造函数中编写事件侦听器,然后您可以随意访问以下(以及更多):

    SC.sound.play(); SC.sound.pause(); SC.sound.stop(); SC.sound.pause(); SC.sound.playState;

    【讨论】:

      【解决方案4】:

      由于关于这个主题的答案很少,我想我会回答我自己的问题。

      我继续下载了 Sound Manager 2 并将必要的文件放置在需要的位置(您可以在 Sound manager2 页面的基本教程部分找到它。)

      http://www.schillmania.com/projects/soundmanager2/demo/template/

      诀窍在于找到与我想要嵌入的用户关联的 SoundCloud mp3。为此,我必须使用 RESTClient 并输入适当的 api url。 看起来像这样。

      http://api.soundcloud.com/tracks/12758186.json?client_id={MyClient_ID_number}

      http://api.soundcloud.com/tracks/ 是指向轨道 id="12758186" 的通用指针。接下来是我的个人 client_id 的 JSONP 包含,以验证我对 soundcloud 服务器的请求。

      获得此网址后,我在浏览器中进行了尝试,它使用 HTML5 mp3 播放器将我重定向到**另一个网址**,该播放器会自动播放歌曲。

      我从这个 soundcloud 文档中得到了这样做的想法: http://developers.soundcloud.com/docs#playing

      然后我复制了这个 url 并将原始问题中的代码混合到上面提到的基本教程 (http://www.schillmania.com/projects/soundmanager2/demo/template/)。

      最终代码:

                  var is_playing = false;
      
                  $("#stream").live("click", function(){
                      soundManager.setup({
                        url: './swf/soundmanager2.swf',
                        onready: function() {
                          var mySound = soundManager.createSound({
                            id: 'aSound',
                            url: {**another url**},
                            autoplay: false
                          });
                          if (is_playing===false)
                              {
                                  mySound.play();
                                  is_playing = true;
                              }
                          else if (is_playing=== true)    
                              {
                                  mySound.stop();
                                  is_playing = false;
                              }
      
                        },
                        ontimeout: function() {
                          // Hrmm, SM2 could not start. Missing SWF? Flash blocked? Show an error, etc.?
                        }
                      });
                  });
      

      【讨论】:

        【解决方案5】:

        只需将您想要触发暂停函数的事件链接到传递给 SC.stream() 回调函数的“声音”对象。

        以下代码中的表单接受一个声音云链接,例如这个:https://soundcloud.com/djalmix-1/living-in-stereo-dj-almix(因此,一旦应用程序上线并单击加载,请将其粘贴到表单中)。此外,您还必须提供 soundcloud 在注册应用程序时为您提供的自己的 client_id,这只需一分钟左右。

        <!DOCTYPE html>
        <html>
            <head></head>
            <body>
                <script src="http://connect.soundcloud.com/sdk.js"></script>
                <script>
                    function load_sound() {             
                        var track_url = document.getElementById("sc_url").value;
                            SC.initialize({
                                client_id: 'client_id'
                                    });
                                    SC.get('/resolve', {url: track_url}, function(track) {
                                        console.log("the track id is: " + track.id);
                                        SC.stream("/tracks/" + track.id, function(sound) {
                                            var is_playing = true;
                                            sound.play();
                                            document.getElementById("stream").onclick = function(){  
                                                if(is_playing === false){
                                                    sound.resume();
                                                    is_playing = true;
                                                } else if(is_playing === true){
                                                    sound.pause();
                                                    is_playing = false;
                                                }   
                                            };                                  
                                        });
                                    });
                                }              
                </script>
                <form>
                    <input id="sc_url" type="url">
                    <input type="button" onclick="load_sound();" value="load">
                </form>
                <button id="stream">pause/resume</button>
            </body>
        </html>
        

        【讨论】:

          猜你喜欢
          • 2011-09-25
          • 1970-01-01
          • 1970-01-01
          • 2016-04-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-10-06
          相关资源
          最近更新 更多