HTML5 provides a standard for playing audio.
Audio on the Web
Until now, there has never been a standard for playing audio on a web page.Today, most audio are played through a plugin (like flash). However, not all browsers have the same plugins.
HTML5 specifies a standard way to include audio, with the audio element.
The audio element can play sound files, or an audio stream.
Audio Formats
Currently, there are 3 main formats for the audio element:| Format | IE 9 | Firefox 3.5 | Opera 10.5 | Chrome 3.0 | Safari 3.0 |
|---|---|---|---|---|---|
| Ogg Vorbis | No | Yes | Yes | Yes | No |
| MP3 | Yes | No | No | Yes | Yes |
| Wav | No | Yes | Yes | Yes | Yes |
How It Works
To play an audio file in HTML5, this is all you need:| <audio src="song.ogg" controls="controls"> </audio> |
Insert content between the <audio> and </audio> tags for browsers that do not support the audio element:
Example
<audio src="song.ogg" controls="controls">Your browser does not support the audio element.
</audio>
The example above uses an Ogg file, and will work in Firefox, Opera and Chrome.
To make the audio work in Internet Explorer and Safari, add an audio file of the type MP3.
The audio element allows multiple source elements. Source elements can link to different audio files. The browser will use the first recognized format:
Example
<audio controls="controls"><source src="song.ogg" type="audio/ogg" />
<source src="song.mp3" type="audio/mpeg" />
Your browser does not support the audio element.
</audio>
All <audio> Attributes
| Attribute | Value | Description |
|---|---|---|
| autoplay | autoplay | Specifies that the audio will start playing as soon as it is ready. |
| controls | controls | Specifies that controls will be displayed, such as a play button. |
| loop | loop | Specifies that the audio will start playing again (looping) when it reaches the end |
| preload | preload | Specifies that the audio will be loaded at page load, and ready to run. Ignored if autoplay is present. |
| src | url | Specifies the URL of the audio to play |
No comments:
Post a Comment