以下是我的小游戏的摘录(除了声音之外,其他所有工具都起作用,Chrome也没有错误,console.log证明代码已运行)???
<!DOCTYPE html>
<html>
<head>
<title> Final Game Code + HS RA</title>
</head>
<body>
<script>
var myAudio = new Audio();
myAudio.source = "Sounds/Impact_1.mp3";
myAudio.volume = 1;
// a bunch of code for the game that all works goes here including a function that calls the below...
nanonautTouchedARobot = true;
myAudio.load();
myAudio.play(); // play the sound #############################################
// below line is just test code to prove progress on the console
console.log('OUCH!');
// a bit more code for the game that all works goes here...
</script>
</body>
</html>
最佳答案
myAudio.source = "Sounds/Impact_1.mp3";
这是不正确的。您需要
src
属性:myAudio.src = 'Sounds/Impact_1.mp3';
此外,您不需要像这样的
.load()
之前的.play()
。并且,请确保您要在用户操作中调用.play()
,以免运行自动播放策略。