

Typically, this adds a horizontal control bar featuring a play/pause button, a timeline with draggable playhead, and a volume control, much like you see at the bottom of a YouTube video. If we want the sound to play automatically when the page has loaded, then we can add an autoplay attribute to the element: While the above code embeds a sound file, it doesn’t actually play it, so it’s not much use on its own. However, we’ll look at how to support these browsers later in the article. Older versions of Internet Explorer - that is, versions 7 and 8 - don’t even support the audio element. ogg versions of your sound file (tools such as Audacity are handy for this), but it will give you good cross-browser support. Of course, this does mean that you need to create both. The browser will then play the first file that it’s capable of playing.

To do this, you can add multiple elements to the element to specify the same sound file in multiple formats. mp3 files to those browsers that can play them, and. What audio formats does your browser support? Find out with this handy test page.Īs you can see from the table, the only practical way to provide cross-browser support for audio playback is to serve. Here’s a summary of current browser support for various audio formats: Browser wav files, but WAVs are uncompressed, resulting in large file sizes which are impractical for anything other than short audio snippets. ogg files, while other browsers can play. Simple though the above example is, it won’t work in many browsers. Not much explanation needed here! Much like the tag lets you include an image file in a page, the tag includes an audio file. Since it’s the season to be jolly - and Europe’s getting more than its fair share of snow right now - let’s embed a short MP3 snippet of Bing Crosby’s “White Christmas”: The basic audio element is really easy to use. We’ll also take a look at how to trigger and stop the audio via JavaScript, and how to ensure our audio can play on as many web browsers as possible. In this tutorial I’ll show you how to embed sounds in a page with the audio element. (We’ll come back to browser support later!) More importantly, it doesn’t require any plugin to work, and is supported by nearly all modern web browsers. This element lets you embed an audio file in a web page, as well as control playback of the sound using JavaScript. However, this doesn’t help with browsers such as Safari on the iPhone and iPad, which don’t support Flash at all.įortunately, HTML5 looks set to make life easier for us developers, thanks to its audio element. The ubiquity of the Flash plugin has helped to a large extent, since Flash makes it easy to embed audio in a way that works with most browsers.

Traditionally there are several ways to embed a sound in a web page - some work better than others, and many only work if you happen to be using the right browser with the right plugin. Up to now, playing audio in a web browser has been a bit of a black art.
