I would like to make a button play a B-flat scale when clicked. What am I doing wrong?
<!DOCTYPE html>
<html>
<head>
<script>
function PlaySound() {
alert("hello");
var bflat = new audio();
bflat.src = "bflat.mp3";
document.getElementById(bflat);
bflat.Play();
}
</script>
</head>
<body>
<audio id="bflat"> </audio>
<button onclick="PlaySound"> B Flat </button>
</body>
</html>
Use below code, if the melodies are at some other location. In Javascript, provide below:
<script>
function PlaySound(melody) {
alert("On Press of "+melody);
var path = "path\\to\\melody\\"
var snd = new Audio(path + melody + ".mp3");
snd.play();
}
</script>
In HTML body: you can insert
<button onclick="PlaySound('melody1')"> button1</button>
<button onclick="PlaySound('melody2')"> button2</button>
Keep it simple (until it works) and try this:
<audio id="bflat" src="bflat.mp3"></audio>
<button onclick="document.getElementById('bflat').play()">Play!</button>
Found it at MDN
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With