gpt4 book ai didi

javascript - 音频无法在Javascript中使用

转载 作者:行者123 更新时间:2023-12-03 02:02:01 24 4
gpt4 key购买 nike

我正在尝试让音频正常播放,但似乎无法正常工作。我以为是间隔还是如果语句阻止了播放,但是正如您可以看到的,甚至在没有任何声音的情况下就已经有一个 call 。目录在正确的位置,并且文件肯定在这里。页面上还有一个小“<>”符号,我不知道为什么。这是运行脚本的HTML:

<!DOCTYPE html>
<>
<head lang="en">
<meta charset="UTF-8">
<title>Flash/Beepo</title>
Flash/Beep Test
</head>
<button id="b2" onclick=startFlash()>Start</button>
<script src="scripts/Flashbeep.js"></script>
<img src='images/unnamed.jpg' alt='blinking!' id='blink' />
<audio style="visibility:hidden" id="beep" src="sounds/beep.wav"></audio>
<audio style="visibility:hidden" id="2beep" src="sounds/2beep.wav"></audio>
<audio style="visibility:hidden" id="3beep" src="sounds/3beep.wav"></audio>
<audio style="visibility:hidden" id="4beep" src="sounds/4beep.wav"></audio>
<body>

</body>
</html>

这是脚本:
function startFlash(){
var img = document.getElementById('blink');
var i=Math.floor(Math.random()*(3))+1
var count=0
var q= Math.floor(Math.random() +.5)
document.getElementById('beep').play();//no sound
if(q==0) {


var interval = window.setInterval(function () {
if (count == i) {
clearInterval(interval)
}
if (img.style.visibility == 'hidden') {
img.style.visibility = 'visible';
count = count + 1
} else {
img.style.visibility = 'hidden';
}
}, 17);
}else{


var interval = window.setInterval(function () {
if (count == 1) {
clearInterval(interval)
}
if (img.style.visibility == 'hidden') {
img.style.visibility = 'visible';
document.getElementById('beep').play();//no sound
count = count + 1
} else {
img.style.visibility = 'hidden';
}
}, 17);


}
}

最佳答案

这是一个有效的音频代码...我只是将函数声明移到了开始按钮的顶部。如果可以,请尝试到底。

<script>
function startFlash(){
var img = document.getElementById('blink');
var i=Math.floor(Math.random()*(3))+1
var count=0
var q= Math.floor(Math.random() +.5)
document.getElementById('beep').play();//no sound
if(q==0) {


var interval = window.setInterval(function () {
if (count == i) {
clearInterval(interval)
}
if (img.style.visibility == 'hidden') {
img.style.visibility = 'visible';
count = count + 1
} else {
img.style.visibility = 'hidden';
}
}, 17);
}else{


var interval = window.setInterval(function () {
if (count == 1) {
clearInterval(interval)
}
if (img.style.visibility == 'hidden') {
img.style.visibility = 'visible';
document.getElementById('beep').play();//no sound
count = count + 1
} else {
img.style.visibility = 'hidden';
}
}, 17);


}
}
</script>

<button id="b2" onclick=startFlash()>Start</button>
<!--<script src="scripts/Flashbeep.js"></script>-->
<img src='http://www.colgate.fi/ColgateOralCare/ElectricToothbrush/ProClinical/common/images/icons/red-dot.png' alt='blinking!' id='blink' />
<audio style="visibility:hidden" id="beep" src="http://www.freesound.org/data/previews/70/70106_1022651-lq.mp3"></audio>
<audio style="visibility:hidden" id="2beep" src="sounds/2beep.wav"></audio>
<audio style="visibility:hidden" id="3beep" src="sounds/3beep.wav"></audio>
<audio style="visibility:hidden" id="4beep" src="sounds/4beep.wav"></audio>
<body>

</body>

FIDDLE

关于javascript - 音频无法在Javascript中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30716825/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com