我正在尝试播放持续时间的音频数组。我看到一些帖子说Object没有长度所以我决定使用Object.keys而不是for循环。但我得到一些错误'无法将undefined或null转换为对象'和'sound / undefined.wav 404(未找到)'和'无法加载,因为找不到支持的源。'。我哪里做错了?感谢任何帮助。
<button id="subBut" style=display:none onClick="">Start!!</button>
<div id="audioControl">
<button id="play" style=display:none onClick="">Play!!</button>
<button id="pause" style=display:none onClick="">Pause!!</button>
<br>
<div id="counDownTimer" style=dispaly:none></div>
</div>
<script>
$("#enterVariables").on("click", function() {
$('#enterVariables').hide();
startBut.show();
$("#pause").hide();
});
</button>
var playList = [{"duration": 5000, "value": "0_silence"},{"duration":5000, "value": "1_hello"}, {"duration": 5000, "value": "2_how_old"}];
$(document).ready(function() {
$("#play").on("click", function(){
playAudio(playList);
function playAudio(playList) {
playNextNote(playList);
function playNextNote(playList) {
var i = 0;
var playListKeys = Object.keys(playList);
if (i < playListKeys.length) {
//for (i in playList){
var value = playListKeys[i].value;
var audioElement = document.createElement('audio');
audioElement.setAttribute('src', 'sound/'+ value + '.wav');
audioElement.play();
i++;
setTimeout(playNextNote, playListKeys.duration);
}
}
};
});
});
答案 0 :(得分:0)
您的代码可能存在一些问题:
以下是修正
var playList = [{"duration": 5000, "value": "0_silence"},{"duration":5000, "value": "1_hello"}, {"duration": 5000, "value": "2_how_old"}];
$(document).ready(function() {
$("#play").on("click", function(){
playNextNote(playList);
});
});
var i = 0;
function playNextNote(playList) {
if (i < playList.length) {
var audioElement = document.createElement('audio');
audioElement.setAttribute('src', 'sound/'+ playList[i].value + '.wav');
audioElement.play();
setTimeout(playNextNote, playList[i].duration);
i++;
} else {
i = 0; // reset i to zero to start over if button clicked again
}
}
&#13;