2 out of 8 rated this helpful - Rate this topic

audio object

Specifies the sound or audio content, such as music or effects, to be played in a document.

HTML5 A vocabulary and associated APIs for HTML and XHTML, Section 4.8.7Internet Explorer 9

Standards information

Examples

The following code example plays an MP3-format audio file.

Using one of the following formats, type the name of an MP3-format audio file in the text box:

  • Local file syntax: "file://C:\yourfilepath.mp3"
  • Web file syntax: "http://webfilepath.mp3"
No path is needed if your audio file is in the same folder as the source code of the example.

    <title>Simple Audio Example</title>
     <script type="text/javascript">
//  Alternates between play and pause based on the value of the paused property
         function togglePlay() {
             if(document.getElementById("audio1")){
                 var audioElm = document.getElementById("audio1");
                 if (audioElm.paused == true){
                    playAudio(audioElm);    //  if player is paused, then play the file
                 }else{
                    pauseAudio(audioElm);   //  if player is playing, then pause
                 }
             }
         }

         function playAudio(audioElm) {
             document.getElementById("playbutton").innerHTML = "Pause"; // button text == Pause
             //get file from text box and assign it to the source of the audio element 
             audioElm.src = document.getElementById('audioFile').value ; 
             audioElm.play();   
         }

         function pauseAudio(audioElm) {
             document.getElementById("playbutton").innerHTML = "play"; //  button text == Play
             audioElm.pause();
         }    
     </script>
</head>
<body>
<div>
  <audio id="audio1" style="width:25%" controls >Not supported</audio>
</div>
<div>
<input type="text" id="audioFile" size="60" />
</div>
<button id="playbutton" onclick="togglePlay();">Play</button>


 

 

Send comments about this topic to Microsoft

Build date: 2/14/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
MOTW causes confusion with audio support
Like others, I can't get IE9 to play audio at all.  It doesn't throw an exception, it just refuses to play sound.  On top of that, inserting MOTW to allow unit testing for scripts on a local workstation causes IE9 to disallow audio support.  It's just that little bit too confusing. $0$0 $0 $0I find that Chrome is also not working with audio, though, so perhaps I'm doing this completely wrong.$0
ie9 with audio element?
Has anyone been able to get the audio tag to work with IE9.
I have been trying with no success. The example shown here fails at least for me.
Can anyone point me in the right direction?
Cheers,
GGW
  • 3/19/2011
  • GGW
Support for WAV files
It doesn't even support WAV files! It's the simplest audio format ever!! It's sad, but the HTML 5 spec is already doomed. Each major browser now supports a different set of audio and video formats. Thanks a lot, to all of you. As my friend said: "Standardization is a very private thing...".
No ogg support!
There's no support for ogg. This makes me sad. The source is open, so there's no excuse not to support it.
IE 9 beta does not support "new Audio()" javascript constructor
Unfortunately the only way I have found to create audio element via script is by call to document.createElement("audio"). Much cleaner call to new Audio() constructor method is not supported: simply returns null.