First you’ll need to drag your audio files into the resource folder, and create an audio element. Insert this in the inner HTML of a rectangle:
<audio id="cChord" width="400" controls="controls" preload="auto">
<source src="${resourcesFolderName}/cChord.mp3" type="audio/mpeg"> <!-- Safari and iPhone -->
<source src="${resourcesFolderName}/cChord.ogg" type="audio/ogg"> <!-- Firefox and Chrome -->
</audio>
This can be off the scene area. It basically makes an audio element which can be targeted using the ‘getelementbyid’ command.
So with the ‘id’ set above, you can then target it with this code:
var myAudio = document.getElementById("cChord");
var key = function (e) {
e = e || window.event;
var k = e.keyCode || e.which;
// pressing the right arrow will pause or play your audio element...
if (k == 39) {
if (myAudio.paused);
myAudio.play();
else
myAudio.pause();
};
};
// for more key codes, view http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes
There’s a bit more info about working with audio here: Controlling Audio & Advanced Techniques (Play, Pause, Rewind, Volume control)