使用soundcloud spi为不同的声音创建不同的流

时间:2015-12-07 21:15:15

标签: javascript api sdk soundcloud

我的页面上有三个五个播放按钮,每个都有相同的课程,但ID不同

<img class="song" id="0001" src="images/play-button.png">
<img class="song" id="0002" src="images/play-button.png">
<img class="song" id="0003" src="images/play-button.png">
<img class="song" id="0004" src="images/play-button.png">
<img class="song" id="0005" src="images/play-button.png">

我想从soundcloud打开每个音轨的新流(每个img id是音轨ID)...到目前为止我所能做的就是有一首歌曲播放并在按下任何按钮时暂停代码:

var i = $('.song').attr('id');
var is_playing = false;
var player;

SC.stream('/tracks/'+i).then(function(stream){
        player = stream;
    });

$('.song').click(function(){
    if(is_playing == false){
        player.play();
        is_playing = true;
    } else {
        player.pause();
        is_playing = false;
    }
});

是否可以在新流启动时停止上一个流?提前谢谢......

1 个答案:

答案 0 :(得分:1)

保护currentPlaying中的#id(例如),现在您可以检查是否必须打开新流或播放旧流

$('.song').each(function() {
  var i = $(this).attr('id');
  $(this).click(function(){
    if(is_playing == false){
        if(currentPlaying != i) {
                if(player) player.pause();
                SC.stream('/tracks/'+i).then(function(stream){
                    player = stream;
                });
        }
        player.play();
        is_playing = true;
    } else {
        player.pause();
        is_playing = false;
    }
  });
});