这是一个 HTML5 解决方案。据说IE9不能播放wav文件。此外,来自 wav-sounds.com 的一些 wav 文件会引发解码错误(在 FF14 中),因为示例我使用了不同的声音 url。为了简化随机选择,这将 listOfWords 声明为数组数组。希望能帮助到你。
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
var listOfWords = [
["mat","http://www.wav-sounds.com/various/applause.wav"],
["cat", "http://www.wav-sounds.com/various/bomb.wav"],
["dog", "http://www.wav-sounds.com/various/bark.wav"],
["pit", "http://www.wav-sounds.com/various/beep.wav"],
["pot", "http://www.wav-sounds.com/various/cashtill.wav"]
];
function playRandom() {
var wordWavUrl = listOfWords[Math.floor(listOfWords.length * Math.random())];
var word = wordWavUrl[0];
var url = wordWavUrl[1];
document.getElementById("wordDisplay").innerHTML=word;
document.getElementById("audioPlay").src=url;
document.getElementById("audioPlay").autoplay="autoplay";
}
</script>
</head>
<body>
<div id="wordDisplay"></div>
<button onclick="playRandom();">play random</button>
<audio id="audioPlay">
</audio>
</body>
</html>