Javascript HTML5音频暂停不工作

Javascript HTML5音频暂停不工作,javascript,html,audio,Javascript,Html,Audio,这是目前没有暂停的音频,而这工作正常播放它 document.getElementById('s/' + currentRadio + '/' + currentSong).pause(); 如果我在js控制台上手动输入暂停代码,音频将停止。。。所以不知道发生了什么 如果我试试这个 document.getElementById('s/' + currentRadio + '/' + currentSong).play(); 我去拿潜水器,这样一切都会好起来的 console.log(doc

这是目前没有暂停的音频,而这工作正常播放它

document.getElementById('s/' + currentRadio + '/' + currentSong).pause();
如果我在js控制台上手动输入暂停代码,音频将停止。。。所以不知道发生了什么

如果我试试这个

document.getElementById('s/' + currentRadio + '/' + currentSong).play();
我去拿潜水器,这样一切都会好起来的

console.log(document.getElementById('s/' + currentRadio + '/' + currentSong));

已更新

问题在于条件检查。如果不考虑运行,则两个
。这意味着当满足第一个条件时(
…&¤tPlay
currentPlay
被设置为
false
,音频暂停

然后,当第二个检查在独立(
!currentPlay&&…
)之后运行时,
currentPlay
已经是
false
,因此也满足此条件,然后立即触发
play()
,使其看起来好像暂停不起作用

通过使用如下的
else
语句进行求解:

$('#tuneRadioPause').click(function(e) {
    if(currentSong !== -1 && currentPlay) {
        console.log("s/" + currentRadio + "/" + currentSong)
        document.getElementById('s/' + currentRadio + '/' + currentSong).pause();
        currentPlay = false;
    }

    if(!currentPlay && currentSong !== -1) {
        document.getElementById('s/' + currentRadio + '/' + currentSong).play();
        currentPlay = true;
    }
});
我建议使用音频元素的实际状态来检查,而不是使用未绑定的标志,例如:

$('#tuneRadioPause').click(function(e) {
    if(currentSong !== -1 && currentPlay) {
        console.log("s/" + currentRadio + "/" + currentSong)
        document.getElementById('s/' + currentRadio + '/' + currentSong).pause();
        currentPlay = false;
    }

    else if(!currentPlay && currentSong !== -1) {          // else here!!
        document.getElementById('s/' + currentRadio + '/' + currentSong).play();
        currentPlay = true;
    }
});
$('tuneRadioPause')。单击(函数(e){
如果(currentSong<0 | | currentRadio<0)返回;
var audio=document.getElementById('s/'+currentRadio+'/'+currentSong);
if(null==audio | | audio.readyState===audio.HAVE_NOTHING)返回;
如果(!audio.paused&&!audio.end){
audio.pause();
}
else if(音频暂停){
音频播放();
}
//currentPlay=!audio.paused;//不再需要,但可能在其他地方?
});
var currentSong=0,currentRadio=0;
document.getElementById('s/'+currentRadio+'/'+currentSong).oncanplay=function(){$('tuneRadioPause')[0]。disabled=false}
$(“#调谐无线电暂停”)。单击(功能(e){
如果(currentSong<0 | | currentRadio<0)返回;
var audio=document.getElementById('s/'+currentRadio+'/'+currentSong);
if(null==audio | | audio.readyState===audio.HAVE_NOTHING)返回;
如果(!audio.paused&&!audio.end){
audio.pause();
}
else if(音频暂停){
音频播放();
}
//currentPlay=!audio.paused;//不再需要,但可能在其他地方?
})


播放/暂停
尝试使用以下方法:

$('#tuneRadioPause').click(function(e) {
    if (currentSong < 0 || currentRadio < 0) return;

    var audio = document.getElementById('s/' + currentRadio + '/' + currentSong);
    if (null === audio || audio.readyState === audio.HAVE_NOTHING) return;

    if(!audio.paused && !audio.ended) {
        audio.pause();
    }
    else if (audio.paused) {
        audio.play();
    }

    //currentPlay = !audio.paused;  // not needed anymore, but may somewhere else?
});

如果有的话,你能不能也给我看一下父div?除了按钮的相关html外,脚本的放置似乎是一个
窗口。onload
问题。您的javascript是否包装在
窗口中。onload
?如果您使用的是jQuery,则可能需要使用
$(window.load
)。但是如果暂停是由用户事件触发的,那么这无关紧要
$('#tuneRadioPause').click(function(e) {
    if(currentSong !== -1 && currentPlay) {
        console.log("s/" + currentRadio + "/" + currentSong)
        document.getElementById('s/' + currentRadio + '/' + currentSong).pause();
        currentPlay = false;
    }

    else if(!currentPlay && currentSong !== -1) {          // else here!!
        document.getElementById('s/' + currentRadio + '/' + currentSong).play();
        currentPlay = true;
    }
});
$('#tuneRadioPause').click(function(e) {
    if (currentSong < 0 || currentRadio < 0) return;

    var audio = document.getElementById('s/' + currentRadio + '/' + currentSong);
    if (null === audio || audio.readyState === audio.HAVE_NOTHING) return;

    if(!audio.paused && !audio.ended) {
        audio.pause();
    }
    else if (audio.paused) {
        audio.play();
    }

    //currentPlay = !audio.paused;  // not needed anymore, but may somewhere else?
});
var audio = document.getElementById("yourAudio"); 

function pauseAudio() { 
    audio.pause(); 
}