i have written some simple html and javascript that should play a sound file. The page displays the two buttons but when i click the play button it does nothing . I know that my browser can play my mp3 file because it works with just the audio tag ex:
<audio src="soundtest.mp3" ></audio>
If anyone could tell me what i am doing wrong i would greatly appreciate it.
<!DOCTYPE html>
<html>
<body>
<button onclick="playSong()" type="button">play</button>
<button onclick="pauseSong()" type="button">pause</button>
<br>
<audio id="audio1">
<source src="soundtest.mp3" type="audio/mp3" >
Your browser does not support the audio tag!
</audio>
<script>
var mySong=document.getElementById("audio1");
function playSong()
{
mySong.play();
}
function pauseSong()
{
mySong.pause();
}
</script>
</body>
</html>
There are a few things to try. First and foremost, check
mySong.canPlayType("audio/mpeg"). It should return “maybe”. If it does not, your browser doesn’t support MP3 and you should try a different one.Then, the
<audio>tag’stypeattribute should beaudio/mpeg, notaudio/mp3. MIME type is important.So important, in fact, that if it still doesn’t work you should check to ensure that the server is configured to serve
.mp3files with theaudio/mpegMIME type header.If this still doesn’t work… try turning the speakers on :p I’m kidding, if you still have problems after these steps, let me know.