当我点击按钮时,我的声音正在播放,但是当我重新点击它时它不会暂停或停止。显示else块的警报正在工作,所以我认为它是第二个sc.stream。见下面的代码:
var i = $('.song').attr('id');
var is_playing = false;
$('.song').click(function(){
if(is_playing == false){
SC.stream('/tracks/'+i).then(function(player){
player.play();
});
is_playing = true;
} else {
SC.stream('/tracks/'+i).then(function(player){
player.pause();
});
alert('This alert works when is_playing is true');
is_playing = false;
}
});
答案 0 :(得分:1)
您当前正在创建两个单独的流(以及两个单独的player
个对象)。您需要先创建播放器并使其在回调之外可用,例如将其存储在全局变量中:
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();
alert('This alert works when is_playing is true');
is_playing = false;
}
});