繁体   English   中英

在我的简单 javascript 游戏中使用音频的声音无法播放

[英]Sounds using audio in my simple javascript game won't play

以下是我的小游戏的一段摘录(除了声音,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';

此外,在 .play( .load()之前.play() 并且,确保您在用户操作上调用.play() ,这样您就不会违反自动播放策略。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM