希望你能帮我。我现在困在这个问题上好几天了,解决不了。使用BTNReminder按钮,我设置了一个定时器。当时机成熟时,AudioElement播放我设定的秒。现在我试图阻止声音,但我不能赶上AudioElement。我试过把整张表都弄掉了,但那也没什么用。理想上,我想做一些功能,把音频元素按一下,就像这个audioElement.pause();
这个是可能的吗?我犯了什么错误吗?
求助请求:-/
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<title>Major Support</title>
<script
src="http://code.jquery.com/jquery-latest.min.js"
type="text/javascript"
></script>
</head>
<div class="buttonWrapperDiv">
<h3 id="minutes">00:00</h3>
<span id="count"></span>
<button id="stopBTNtwo">STOP</button>
</div>
<div class="btnWrapper">
<button id="BTNReminder" value="5">5 seconds</button>
</div>
</div> <script>
$(function () {
function countdownReal(number) {
$("#").html(number);
if (number === 0) {
d.resolve();
} else {
number -= 1;
window.setTimeout(function () {
countdownReal(number);
}, 1000);
}
return d.promise();
}
function waitForAudioToFinish(audioElement) {
if (!audioElement.paused) {
setTimeout(function () {
waitForAudioToFinish(audioElement);
}, 200);
} else {
d1.resolve();
}
}
function playSong(src) {
var audioElement = document.createElement("audio");
audioElement.setAttribute("autoplay", "autoplay");
audioElement.setAttribute("id", "beepbeep");
audioElement.setAttribute("value", "test");
audioElement.setAttribute("src", src);
audioElement.play();
waitForAudioToFinish(audioElement);
function mute(audioElement){
audioElement.muted = true;
audioElement.pause();
}
return d1.promise();
}
var d = new $.Deferred();
var d1 = new $.Deferred();
$("#BTNReminder").click(function () {
var fired_button = $(this).val();
$("#BTNReminder").on("click", function () {
function countdown(fired_button) {
var seconds = 5;
var mins = 1;
function tick() {
var minutenCounter = document.getElementById("minutes");
var current_minutes = mins - 1;
seconds--;
minutenCounter.innerHTML =
current_minutes.toString() +
":" +
(seconds < 10 ? "0" : "") +
String(seconds);
if (seconds > 0) {
setTimeout(tick, 1000);
} else {
if (mins > 1) {
countdown(mins - 1);
}
}
}
tick();
}
countdown(fired_button);
function countdownZwei(fired_button) {
var seconds = 5;
var mins = 1;
function tick() {
var minutenCounter = document.getElementById("minutes");
var current_minutes = mins - 1;
seconds--;
minutenCounter.innerHTML =
current_minutes.toString() +
":" +
(seconds < 10 ? "0" : "") +
String(seconds);
if (seconds > 0) {
setTimeout(tick, 1000);
} else {
if (mins > 1) {
countdown(mins - 1);
}
}
}
tick();
}
countdownZwei(fired_button);
$(this).prop("disabled", true);
$.when(countdownReal(fired_button)).then(function () {
playSong("audio.mp3").then(function () {
$(".BTNmajor").prop("disabled", false);
});
});
});
});
});
</script>
</body>
</html>发布于 2022-01-09 16:22:12
我用这种方法解决了这个问题:
var audioElement = document.createElement("audio");
function playSong(src) {
audioElement.setAttribute("autoplay", "autoplay");
audioElement.setAttribute("src", src);
audioElement.play();
waitForAudioToFinish(audioElement);
return d1.promise();
}
$("#stopBTN").click(function () {
audioElement.pause()
});https://stackoverflow.com/questions/70630697
复制相似问题