我想让一个按钮在单击时播放降 B 音阶。我做错了什么?
<!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>
最佳答案
如果旋律位于其他位置,请使用以下代码。 在 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>
在 HTML 正文中:您可以插入
<button onclick="PlaySound('melody1')"> button1</button>
<button onclick="PlaySound('melody2')"> button2</button>
关于javascript - 单击一个按钮以在 Javascript 上播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32913026/