我在 js 中有一个函数可以检查可变日期,如果存在,它将启动一个音频文件。
由于使用新的隐私,不可能自动启动音频文件,事实上在我的情况下它被阻止了。我希望浏览器框显示同意复制音频文件。
但是,我不知道该怎么做。你能帮助我吗?
var audio = new Audio('/sound/file-one.mp3');
setInterval(function() {
$.ajax({
url: '/check_data.php',
type: 'post',
data: {
'check_data' : 1
},
success: function(response){
if (response != "false") {
audio.addEventListener('ended', function () {
audio.play();
}, false);
audio.play();
}
}
});
}, 5000);
Run Code Online (Sandbox Code Playgroud)
小智 6
用户必须启动音频,通过用户操作启动音频后,您可以随时更改源。
const audio = document.getElementById("au");
let bt = document.getElementById("bt");
console.log(audio);
bt.addEventListener("click", ()=>{
audio.play();
});
const startPlaying = ()=>{
audio.removeEventListener('playing', startPlaying);
bt.classList.add("hide");
audio.src = 'https://freesound.org/data/previews/475/475736_4397472-lq.mp3';
audio.play();
}
audio.addEventListener('playing', startPlaying);
audio.addEventListener('error', ()=>{
console.log("error");
});Run Code Online (Sandbox Code Playgroud)
.hide{
display:none;
}Run Code Online (Sandbox Code Playgroud)
<input id="bt" type="button" value="Accept">
<audio id="au" preload="auto" src="https://raw.githubusercontent.com/anars/blank-audio/master/500-milliseconds-of-silence.mp3"></audio>Run Code Online (Sandbox Code Playgroud)