Click a button to play sound on Javascript

Liv*_*ini 4 html javascript

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>
Run Code Online (Sandbox Code Playgroud)

AB *_*bhi 5

如果旋律位于其他位置,请使用以下代码。在 Javascript 中,提供以下内容:

<script>
    function PlaySound(melody) {
        alert("On Press of "+melody);
        var path = "path\\to\\melody\\"
        var snd = new Audio(path + melody + ".mp3");
        snd.play();
    }
</script>
Run Code Online (Sandbox Code Playgroud)

在 HTML 正文中:您可以插入

<button onclick="PlaySound('melody1')"> button1</button>
<button onclick="PlaySound('melody2')"> button2</button>
Run Code Online (Sandbox Code Playgroud)