Sound Cloud javascript API Stop()方法不起作用

时间:2013-03-02 07:04:42

标签: javascript sdk streaming soundcloud

我正在尝试使用以下代码切换启动和停止Sound Cloud流。 play()方法作为条件逻辑用作墙。但是stop()方法不是。谁能解释我做错了什么?

SC.initialize({
  client_id: 'MY_CLIENT_ID'
});

  if (streamingBool) {
      SC.stream("/tracks/" + myTrackId, function (sound1) {
          sound1.stop();
      });
      streamingBool = false;
  } else {
      SC.stream("/tracks/" + myTrackId, function (sound) {
          sound.play();
      });
      streamingBool = true;
  }

1 个答案:

答案 0 :(得分:0)

此代码存在两个问题

  • SC.stream异步执行其函数回调
  • 变量范围,即您试图停止声音而不是正在播放的声音

工作实施将是:

SC.initialize({
  client_id: 'MY_CLIENT_ID'
});

var playing = false;    

// Play a track
play = function(myTrackId){
  if(playing){
    SC.sound.stop();
  }

  SC.stream("/tracks/" + myTrackId, function(sound){
    // Store the sound object inside the SC object which belongs 
    // to the global scope so that it can be accessed out of the 
    // scope of this callback
    SC.sound = sound;
    SC.sound.play();
    playing = true;
  });
}

// Stop the currently playing track
stop = function(){
  if(playing){
    SC.sound.stop();
  }
}