我正在开发一个音频播放器的主控制面板,一切正常,除非我播放另一首曲目,而一首曲目已经播放,然后在主控制上点击停止,然后再在主控制器上再次播放它没有&#39 ; t添加课程" .pause"回到赛道。 JsFiddle:http://jsfiddle.net/jeffd/2fjnmdkb/15/
$(".play").on('click', function () {
var key = $(this).attr('key');
var nowplay = $(this); // variable for now playing class .play
EvalSound(this, key);
$(".play").not(this).removeClass("pause");
$(this).toggleClass("pause");
$(this).hasClass("pause") ? $(".playerbottom").addClass("pausebottom") : $(".playerbottom").removeClass("pausebottom");
$(".playerbottom").on('click', function () {
nowplay.toggleClass("pause");
$(".play").not(nowplay).removeClass("pause");
});
});
var thissound = new Audio();
var currentKey;
function EvalSound(el, key) {
thissound.addEventListener('ended', function () {
// done playing
$(el).removeClass("pause");
$(".playerbottom").removeClass("pausebottom");
});
if (currentKey !== key) thissound.src = "http://99centbeats.com/1e4cb5f584d055a0992385c1b2155786/" + key;
currentKey = key;
if (thissound.paused) thissound.play();
else thissound.pause();
currentPlayer = thissound;
}
$(".volume_slider").slider({
value : 75,
step : 1,
range : 'min',
min : 0,
max : 100,
slide : function(){
var value = $(".volume_slider").slider("value");
thissound.volume = (value / 100);
}
});
$(".playerbottom").on('click', function () {
$(this).toggleClass("pausebottom");
if (thissound.paused) thissound.play();
else thissound.pause();
});
答案 0 :(得分:0)
使nowplay成为一个全局变量后,它起作用:window.nowplay = $(this);
工作解决方案:http://jsfiddle.net/jeffd/2gbz7agp/
$(".play").on('click', function () {
var key = $(this).attr('key');
window.nowplay = $(this); // variable for now playing class .play
EvalSound(this, key);
$(".play").not(this).removeClass("pause");
$(this).toggleClass("pause");
$(this).hasClass("pause") ? $(".playerbottom").addClass("pausebottom") : $(".playerbottom").removeClass("pausebottom");
$(".playerbottom").on('click', function () {
$(this).hasClass("pausebottom") ? nowplay.addClass("pause") : nowplay.removeClass("pause");
});
});
var thissound = new Audio();
var currentKey;
function EvalSound(el, key) {
thissound.addEventListener('ended', function () {
// done playing
$(el).removeClass("pause");
$(".playerbottom").removeClass("pausebottom");
});
if (currentKey !== key) thissound.src = "http://99centbeats.com/1e4cb5f584d055a0992385c1b2155786/" + key;
currentKey = key;
if (thissound.paused) thissound.play();
else thissound.pause();
currentPlayer = thissound;
}
$(".volume_slider").slider({
value : 75,
step : 1,
range : 'min',
min : 0,
max : 100,
slide : function(){
var value = $(".volume_slider").slider("value");
thissound.volume = (value / 100);
}
});
$(".playerbottom").on('click', function () {
$(this).toggleClass("pausebottom");
if (thissound.paused) thissound.play();
else thissound.pause();
});