2

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>
3
  • what does the browser say? please define "not playing" Commented Dec 30, 2012 at 3:27
  • I would try type="audio/mpeg" Commented Dec 30, 2012 at 3:29
  • that was it akonsu thanks!!! I have another issue it seems to work on my safari but not my firefox 17.0.1 Commented Dec 30, 2012 at 3:36

1 Answer 1

3

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's type attribute should be audio/mpeg, not audio/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 .mp3 files with the audio/mpeg MIME 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.

Sign up to request clarification or add additional context in comments.

3 Comments

akonsu told me the same thing about the audio/mpeg...and that was the issue. It works fine now on safari...but for some reason it wont work on firefox 17.0.1
also how would i play multiple songs in a row. ex. clicking the play button and playing multiple songs until the user clicks pause...
Firefox does not support MP3. I cannot fathom the logic in not supporting the single most commonly used file format for sounds... Anyway, if you want a playlist, it's quite involved and you should probably Google some tutorials.

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.