2013-02-28 36 views
2

我想允许声音文件播放和暂停。该文件说明如何播放流文件:如何在soundcloud中暂停流式传输javascript api

$("#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); 
       } 

      }); 

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

+0

'VAR is_playing = “假”'..你应该使用布尔值。 – dfsq 2013-02-28 17:52:49

+1

是的。但是,这并没有解决问题。 – bkbarton 2013-02-28 17:55:10

回答

0

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

我继续和下载的声音管理器2和放置在他们需要进行必要的文件(可以在声音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”。接下来是JSONP包含我的个人client_id以验证我对soundcloud服务器的请求。

一旦我有了这个网址,我在浏览器中尝试了它,并将它重定向到**另一个网址**,并带有一个HTML5 mp3播放器,该播放器自动播放了该歌曲。

我得到了主意,从这个文档的SoundCloud做到这一点: http://developers.soundcloud.com/docs#playing

我然后复制这个网址,并从我原来的问题捣碎的代码上面(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.? 
        } 
       }); 
      }); 
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> 
+0

请正确缩进,否则确实很难阅读源代码,请参阅http://stackoverflow.com/editing-help – 2013-06-25 00:34:46

0

只是链接,你想触发暂停功能被传递到SC.stream()的回调函数的“声音”对象的事件。

以下代码中的表单接受如下的声音云链接:https://soundcloud.com/djalmix-1/living-in-stereo-dj-almix(因此,只要应用程序处于活动状态并单击加载,即粘贴到表单中)。你也必须提供你自己的client_id,当你注册你的应用程序时,soundcloud会给你,这只需要一分钟左右。

<!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> 
1

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

1
SC.stream("/tracks/13680213", function(sound) { 
    SC.sound = sound; 
}); 

这意味着你不是在SC内写入事件监听器。流构造函数,那么你可以随意访问以下(及以上):

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