【发布时间】:2018-10-13 06:24:16
【问题描述】:
我正在尝试用随机问题进行测验。我希望每个问题都附有一个声音片段,该片段会在问题出现后立即自动播放。我还想要一个按钮,让用户可以重播声音剪辑。我怎样才能做到这一点呢?
我知道如何使用audioClips.play(); 播放单个音频片段。
但是如何播放数组中的音频片段,与问题“同步”?
这是我目前所拥有的:
var country = ["Italy", "Spain", "Portugal", "France", "Greece", "Ireland", "Germany"];
var audioClips = ["italy.mp3", "spain.mp3", "portugal.mp3", "france.mp3", "greece.mp3", "ireland.mp3", "germany.mp3"];
var capital = ["rome", "madrid", "lisbon", "paris", "athens", "dublin", "berlin"];
var random001 = Math.floor(Math.random() * country.length);
document.getElementById("country").textContent = country[random001];
function submit001() {
var b = input001.value.toLowerCase();
var text;
if (b === capital[random001]) {
goToNextQuestion();
text = "Correct!";
} else {
text = input001.value.bold() + " is not correct!";
}
document.getElementById("input001").value = "";
document.getElementById("answer001").innerHTML = text;
}
function goToNextQuestion() {
document.getElementById("answer001").innerHTML = "";
random001 = Math.floor(Math.random() * country.length);
document.getElementById("country").innerHTML = country[random001];
document.getElementById("input001").focus();
}
<p id="message001">What is the capital of <text id="country"></text>?</p>
<input type="text" id="input001" autofocus onKeyDown="if(event.keyCode==13) submit001()">
<p id="answer001"></p>
<button onclick="playAudio()" type="button">Replay Audio</button>
【问题讨论】:
-
您将在问题和音频数组中拥有相同数量的条目。如果随机问题在第三个索引上,则使用与音频数组相同的索引。
-
感谢您的帮助。我还在学习 JavaScript,所以如果这是一个菜鸟问题,我很抱歉,但我该怎么做呢?
标签: javascript arrays audio random