I am making a canvas, one where you are a little ship and you shoot little pixels to destroy other little pixel ships. I want to make it so that when you press the key that would make the ship shoot, it would also play a little "pew" sound. I have no idea how to add sounds or images for that matter. Please help.
Asked
Active
Viewed 655 times
0
-
1Have you tried [searching Stack Overflow?](http://stackoverflow.com/questions/9419263/playing-audio-with-javascript) There are [plenty of resources](http://stackoverflow.com/questions/4416505/how-to-take-keyboard-input-in-javascript) that will help you. – Mike Cluck Aug 19 '16 at 19:30
-
Canvas does not include a sound api, you would need to use the audio tag in standard html for that. – QBM5 Aug 19 '16 at 20:23
1 Answers
0
There is an audio tag in HTML. Put any sounds you'll be using within the tag like so:
<audio>
<source id="pew" src="pew.mp3"></source>
</audio>
In Javascript, there is a standard method called play():
function playSound(){
var sound = document.getElementById("pew");
sound.play();
}
Call the function either in HTML or with JQuery:
<input type="button" value="pew pew pew" onClick="playSound()">

Jeremy
- 149
- 2
- 14