4

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>
2
  • I'm not yelling, I'm just frustrated :/ Commented Oct 2, 2015 at 18:14
  • 1
    onclick="PlaySound" won't work... that is effectively just a reference to the function. To call a function you need to use () so change it to onclick="PlaySound()" Commented Oct 2, 2015 at 18:28

5 Answers 5

5

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>
Sign up to request clarification or add additional context in comments.

Comments

4

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

3 Comments

does it matter where the mp3 file is held?
@LivyGolini It matters that you don't create a new <audio> element when the button is clicked, because the newly created element may not be able to start playback immediately (especially the first time).
@LivyGolini: The example in my answer should work, if the MP3 file resides in the same folder as the HTML file. Please test this locally (putting MP3 and HTML file in a directory on your computer and open the HTML file with a browser) before uploading it to a server to avoid other pitfalls. Use the developer output to get more infos about maybe occurring errors (e.g. with Firefox: Take a look at the Tools -> Web Developer menu).
2

This works:

<!DOCTYPE html>
<html>
<head>
<script>
var bflat = new Audio();
bflat.src = "bflat.mp3";
function PlaySound() {
    bflat.play();
}
</script>
</head>
<body>
<audio id="bflat"> </audio>
<button onclick="PlaySound()"> B Flat </button>
</body>
</html>

Comments

0

This is very simple, try to call PlaySound function using this HTML

<button onclick="PlaySound()"> B Flat </button>

Keep it simple!

Comments

0
<!DOCTYPE html>
<html>
<body>

<audio id="myAudio">
  <source src="horse.ogg" type="audio/ogg">
  <source src="horse.mp3" type="audio/mpeg">
  Your browser does not support the audio element.
</audio>

<p>Click the buttons to play or pause the audio.</p>

<button onclick="playAudio()" type="button">Play Audio</button>
<button onclick="pauseAudio()" type="button">Pause Audio</button> 

<script>
var x = document.getElementById("myAudio"); 

function playAudio() { 
    x.play(); 
} 

function pauseAudio() { 
    x.pause(); 
} 
</script>

</body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.