gpt4 book ai didi

Javascript - 播放声音以及数组中的随机值

转载 作者:行者123 更新时间:2023-12-03 00:56:59 26 4
gpt4 key购买 nike

我正在尝试用随机问题进行测验。我希望每个问题都附有一个声音剪辑,该声音剪辑会在问题出现后立即自动播放。我还希望有一个按钮,使用户可以重播声音剪辑。我究竟怎样才能实现这一目标?

我知道如何使用 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>

最佳答案

对于这个问题,您需要根据数组来思考,并且需要根据对象来思考。将相关信息组合到一个对象中,您可以通过清晰、易于阅读的代码来实现您的目标。

为了完成这项工作,我必须广泛重构您的代码。这就是我的想法。

HTML

<head>
<style>
#startButton {
display: block;
}

#quiz {
display: none;
}
</style>
</head>
<body>
<div id="startButton">
<button type="button" onclick="startQuiz()">Start Quiz</button>
</div>
<div id="quiz">
<p id="message001">What is the capital of <text id="country"></text>?</p>
<input type="text" id="input001" autofocus onKeyDown="if(event.keyCode==13) checkAnswer()" value="">
<p id="answer001"></p>
<button id="replayButton" type="button" onclick="setAudio()">Replay Audio</button>
</div>
</body>

Javascript

<script>
var country = {
"Italy": {
"audio": "italy.mp3",
"capital": "rome"
},
"Spain": {
"audio": "spain.mp3",
"capital": "madrid"
},
"Portugal": {
"audio": "portugal.mp3",
"capital": "lisbon"
},
"France": {
"audio": "france.mp3",
"capital": "paris"
},
"Greece": {
"audio": "greece.mp3",
"capital": "athens"
},
"Ireland": {
"audio": "ireland.mp3",
"capital": "dublin"
},
"Germany": {
"audio": "germany.mp3",
"capital": "berlin"
}
};

var countryArray = Object.keys(country);

function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;

}

function nextCountryName(){
let randIndex = getRandomInt(1, countryArray.length) - 1;
return countryArray[randIndex];
};

function playAudio(file){
let playFile = new Audio(file);
playFile.play();
}

function newCountry(newCountryName) {

document.getElementById("country").textContent = newCountryName;
document.getElementById("input001").value = "";
}

function isAnswerCorrect(answer, useCountry) {
let answerId = document.getElementById("answer001");
let ans = answer.toLowerCase();
let text;

if(ans == country[useCountry].capital){
answerId.innerHTML = "Correct!";
return true;
} else {
answerId.innerHTML = ans.bold() + " is not correct!";
return false;
}
};

function setAudio(){
let thisCountry = document.getElementById("country").textContent;
let audioFile = country[thisCountry].audio;
let callStr = "playAudio(\'" + audioFile + "\')";
document.getElementById('replayButton').setAttribute('onclick', callStr);
}

function checkAnswer(){
let inputId = document.getElementById('input001');
let answer = inputId.value;
let thisCountry = document.getElementById("country").textContent;
let audioFile = country[thisCountry].audio;
let result = isAnswerCorrect(answer, thisCountry);

if(result) {
let cntryName = nextCountryName();
newCountry(cntryName);
playAudio(country[cntryName].audio);
}
};

function startQuiz(){
let startingCountry = nextCountryName();
newCountry(startingCountry);

document.getElementById('startButton').style.display = "none";
document.getElementById('quiz').style.display = "block";

playAudio(country[startingCountry].audio);
};
</script>
  • 我将 varcountryvaraudioClipsvarcapital 数组转换为名为 varcountry 的单个对象。这样您就可以使用 country.capitalcountry.audio 来获取您要查找的值。
  • 根据this answer建议您在调用 .play() 方法之前设置音频文件类型 new Audio("file_name")

虽然answer002answer003input002input003等超出了您的问题范围,但此代码可以轻松修改接受此类参数以进行更广泛的测验。

关于Javascript - 播放声音以及数组中的随机值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52784387/

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