HTML5 audio
HTML5 Audio is a subject of the HTML5 specification, incorporating audio input, playback, and synthesis, as well as speech to text, in the browser.
HTML |
---|
Comparisons |
|
<audio> element
The <audio> element represents a sound, or an audio stream.[1] It is commonly used to play back a single audio file within a web page, showing a GUI widget with play/pause/volume controls.
The <audio> element has these attributes:
- global attributes (accesskey; class; contenteditable; contextmenu; dir; draggable; dropzone; hidden; id; lang; spellcheck; style; tabindex; title; translate)
- autoplay = "autoplay" or "" (empty string) or empty
Instructs the User-Agent to automatically begin playback of the audio stream as soon as it can do so without stopping. - preload = "none" or "metadata" or "auto" or "" (empty string) or empty
Represents a hint to the User-Agent about whether optimistic downloading of the audio stream itself or its metadata is considered worthwhile.- "none": Hints to the User-Agent that the user is not expected to need the audio stream, or that minimizing unnecessary traffic is desirable.
- "metadata": Hints to the User-Agent that the user is not expected to need the audio stream, but that fetching its metadata (duration and so on) is desirable.
- "auto": Hints to the User-Agent that optimistically downloading the entire audio stream is considered desirable.
- controls = "controls" or "" (empty string) or empty
Instructs the User-Agent to expose a user interface for controlling playback of the audio stream. - loop = "loop" or "" (empty string) or empty
Instructs the User-Agent to seek back to the start of the audio stream upon reaching the end. - mediagroup = string
Instructs the User-Agent to link multiple videos and/or audio streams together. - muted = "muted" or "" (empty string) or empty
Represents the default state of the audio stream, potentially overriding user preferences. - src = non-empty [URL] potentially surrounded by spaces
The URL for the audio stream.
Example:[2]
<audio controls>
<source src="https://media.w3.org/2010/07/bunny/04-Death_Becomes_Fur.mp4" type="audio/mp4" />
<source src="https://media.w3.org/2010/07/bunny/04-Death_Becomes_Fur.oga" type="audio/ogg; codecs=vorbis" />
<p>Your user agent does not support the HTML5 Audio element.</p>
</audio>
Supporting browsers
On PC:
On mobile devices:
- Android Browser 2.3
- Blackberry Browser
- Google Chrome
- Internet Explorer Mobile 9
- Safari 4[3]
- Firefox
- Opera Mobile 11
Supported audio coding formats
The adoption of HTML5 audio, as with HTML5 video, has become polarized between proponents of free and patent-encumbered formats. In 2007, the recommendation to use Vorbis was retracted from the specification by the W3C together with that to use Ogg Theora, citing the lack of a format accepted by all the major browser vendors.
Apple and Microsoft support the ISO/IEC-defined formats AAC and the older MP3. Mozilla and Opera support the free and open, royalty-free Vorbis format in Ogg and WebM containers, and criticize the patent-encumbered nature of MP3 and AAC, which are guaranteed to be “non-free”. Google has so far provided support for all common formats.
Most AAC files with finite length are wrapped in an MPEG-4 container (.mp4, .m4a), which is supported natively in Internet Explorer, Safari, and Chrome, and supported by the OS in Firefox and Opera.[4] Most AAC live streams with infinite length are wrapped in an Audio Data Transport Stream container (.aac, .adts), which is supported by Chrome, Safari, Firefox and Edge.[5][6][7]
Many browsers also support uncompressed PCM audio in a WAVE container.[8]
In 2012, the free and open royalty-free Opus format was released and standardized by IETF. It is supported by Mozilla, Google, Opera and Edge.[9][10][11][12]
This table documents the current support for audio coding formats by the <audio>
element.
Format | Container | MIME type | Chrome | Internet Explorer | Edge | Firefox | Opera | Safari |
---|---|---|---|---|---|---|---|---|
PCM | WAV | audio/wav | Yes | No | Yes | Yes, in v3.5 | Yes, in v11.00 | Yes, in v3.1 |
MP3 | MP3 | audio/mpeg | Yes[13] | Yes, in IE9 | Yes | Yes, in v71[14] | Yes[13] | Yes, in v3.1 |
AAC | MP4 | audio/mp4 | Yes | Yes, in IE9 | Yes | From OS[lower-alpha 1] | Yes | Yes |
ADTS[lower-alpha 2] | audio/aac audio/aacp | Yes | No | Yes | From OS[lower-alpha 1] in v45.0 | Yes | Yes | |
Vorbis | Ogg | audio/ogg | Yes, in v9 | No | In v79[16] In v17, with Web Media Extensions[17] | Yes, in v3.5 | Yes, in v10.50 | With Xiph QuickTime Components (macOS 10.11 and earlier) |
WebM | audio/webm | Yes | No | In v79[16] In v17, with Web Media Extensions[17] | Yes, in v4.0 | Yes, in v10.60 | No | |
Opus | Ogg | audio/ogg | Yes, in v25 (in v31 for Windows) | No | In v79[18] In v17, with Web Media Extensions[17] | Yes, in v15.0 | Yes, in v14 | No |
WebM | audio/webm | Yes | No | In v79[18] In v17, with Web Media Extensions[17] | Yes, in v28.0[19] | Yes | No | |
CAF | audio/x-caf | No | No | No | No | No | Yes, in Safari 11 and macOS High Sierra | |
FLAC | FLAC | audio/flac | Yes, in v56[20] | No | Yes, in v16[21] | Yes, in v51[22] | Yes | Yes, in v11[23] |
Ogg | audio/ogg | Yes, in v56[20] | No | In v79[24] In v17, with Web Media Extensions[17] | Yes, in v51[22] | Yes | No |
Web Audio API and MediaStream Processing API
The Web Audio API specification developed by W3C describes a high-level JavaScript API for processing and synthesizing audio in web applications. The primary paradigm is of an audio routing graph, where a number of AudioNode objects are connected together to define the overall audio rendering. The actual processing will primarily take place in the underlying implementation (typically optimized Assembly / C / C++ code), but direct JavaScript processing and synthesis is also supported.[25]
Mozilla's Firefox browser implements a similar Audio Data API extension since version 4, implemented in 2010 [26] and released in 2011, but Mozilla warns it is non-standard and deprecated, and recommends the Web Audio API instead.[27] Some JavaScript audio processing and synthesis libraries such as Audiolet support both APIs.
The W3C Audio Working Group is also considering the MediaStream Processing API specification developed by Mozilla.[28] In addition to audio mixing and processing, it covers more general media streaming, including synchronization with HTML elements, capture of audio and video streams, and peer-to-peer routing of such media streams.[29]
Supporting browsers
On PC:
- Google Chrome 10[30] (Enabled by default since 14[31])
- Firefox 23 (Enabled by default since 25)
- Opera 15
- Safari 6
- Microsoft Edge 12
On mobile devices:
- Google Chrome for Android 28 (Enabled by default since 29)
- Safari 6 (Has restrictions on use (Muted unless user called))
- Firefox 23 (Enabled by default since 25)
- Tizen
Web Speech API
The Web Speech API aims to provide an alternative input method for web applications (without using a keyboard). With this API, developers can give web apps the ability to transcribe voice to text, from the computer's microphone. The recorded audio is sent to speech servers for transcription, after which the text is typed out for the user. The API itself is agnostic of the underlying speech recognition implementation and can support both server based as well as embedded recognizers.[32] The HTML Speech Incubator group has proposed the implementation of audio-speech technology in browsers in the form of uniform, cross-platform APIs. The API contains both:[33]
- Speech Input API
- Text to Speech API
Google integrated this feature into Google Chrome in March 2011.[34] Letting its users search the web with their voice with code like:
<script type="application/javascript">
function startSearch(event) {
event.target.form.submit();
}
</script>
<form action="http://www.google.com/search">
<input type="search" name="q" speech required onspeechchange="startSearch">
</form>
Supporting browsers
- Safari 6.1 and up [PARTIAL: speech synthesis only; no recognition]
- Google Chrome 25 and up
- Firefox Desktop 44.0 and up (Linux and Mac) / 45.0 and up (Windows) [PARTIAL: speech synthesis only; no recognition; currently requires "media.webspeech.recognition.enable" about:config option to be manually changed to "true"][35][36][37]
See also
Notes
- There is no native support for the AAC codec due to licensing reasons. Decoding of audio files requires the host OS to provide a compatible library.[15]
- An MPEG-4 file contains a header that includes metadata followed by "tracks" which can include video as well as audio data, for example, H.264 encoded Video and AAC encoded Audio. ADTS in contrast is a streaming format consisting of a series of frames, each frame having a header followed by the AAC data.[6]
References
- "HTML5 audio element - W3C". Archived from the original on 2013-06-06. Retrieved 2013-07-02.
- https://www.w3.org/wiki/HTML/Elements/audio
- About HTML5 Audio and Video - Safari HTML5 Audio and Video Guide
- Is it possible to play this stream using HTML5/javascript?
- MP4 container · Issue #95 · karlheyes/icecast-kh · GitHub
- https://developer.apple.com/library/ios/technotes/tn2236/_index.html#//apple_ref/doc/uid/DTS40008748-CH1-SECTION5
- https://bugzilla.mozilla.org/show_bug.cgi?id=1224887
- https://developer.mozilla.org/en-US/docs/Media_formats_supported_by_the_audio_and_video_elements
- https://developer.mozilla.org/en-US/docs/Media_formats_supported_by_the_audio_and_video_elements#Ogg_Opus
- https://www.xiph.org/press/2012/rfc-6716/
- https://hacks.mozilla.org/2012/09/its-opus-it-rocks-and-now-its-an-audio-codec-standard/
- "WebM, VP9 and Opus Support in Microsoft Edge - Microsoft Edge Dev BlogMicrosoft Edge Dev Blog". blogs.windows.com. Retrieved 2017-03-22.
- "Enable mp3 support in Chromium". Google. Retrieved 2018-05-01.
- "Firefox 71.0 release notes". Mozilla. December 3, 2019.
- "Media type and format guide: image, audio, and video content". Mozilla Developer Network. Mozilla. Retrieved 2019-12-06.
- https://developer.microsoft.com/en-us/microsoft-edge/status/vorbisaudiocodec/
- "Introducing the Web Media Extension Package with OGG Vorbis and Theora support for Microsoft Edge". Microsoft Edge Dev Blog. Microsoft. December 5, 2017.
- https://developer.microsoft.com/en-us/microsoft-edge/status/opusaudioplayback/
- https://www.mozilla.org/en-US/firefox/28.0/releasenotes/
- "FLAC codec support for <audio> and WebAudio". Chrome Platform Status. Retrieved 2016-12-27.
- https://developer.microsoft.com/en-us/microsoft-edge/status/flacaudiocodec/
- "Firefox 51 for developers". Mozilla Developer Network. Retrieved 2016-12-27.
- Chaim Gartenberg (June 6, 2017). "Apple reportedly adds support for FLAC lossless audio in iOS 11". The Verge.
- https://developer.microsoft.com/en-us/microsoft-edge/status/oggcontainer/
- Chris Rogers (2012-03-15). "Web Audio API". W3C. Archived from the original on 2012-03-15. Retrieved 2012-07-04.
- "Audio Data API".
- "Introducing the Audio API extension". Mozilla Developer Network. Mozilla. 2012-03-05. Archived from the original on 2014-01-16. Retrieved 2012-07-04.
- "Audio Processing API". W3C. 2011-12-15. Archived from the original on 2011-12-15. Retrieved 2012-07-04.
- Robert O'Callahan (2012-05-31). "MediaStream Processing API". W3C. Retrieved 2012-07-04.
- Web Audio API is now available in Chrome
- Scott Gilbertson (2011-09-19). "Chrome 14 Adds Better Audio, 'Native Client' Support". Webmonkey. Wired. Retrieved 2012-07-04.
- "API draft". Retrieved January 28, 2012.
- "HTML5 Speech API". Retrieved January 28, 2012.
- "Talking to your computer". Retrieved January 28, 2012.
- "Firefox 44 for developers - Mozilla | MDN". Retrieved March 9, 2016.
- "Firefox — Notes (45.0) — Mozilla". Retrieved March 9, 2016.
- "Web Speech API - Web APIs | MDN". Retrieved March 9, 2016.