2013-02-28 37 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;