簡體   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