<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>HTML5 Doctor &#187; codecs</title>
	<atom:link href="http://html5doctor.com/tag/codecs/feed/" rel="self" type="application/rss+xml" />
	<link>http://html5doctor.com</link>
	<description>helping you implement HTML5 today</description>
	<lastBuildDate>Wed, 01 Feb 2012 09:28:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Native Audio in the browser</title>
		<link>http://html5doctor.com/native-audio-in-the-browser/</link>
		<comments>http://html5doctor.com/native-audio-in-the-browser/#comments</comments>
		<pubDate>Wed, 29 Jul 2009 15:00:29 +0000</pubDate>
		<dc:creator>Mark Boas</dc:creator>
				<category><![CDATA[Browser Compatibility]]></category>
		<category><![CDATA[Elements]]></category>
		<category><![CDATA[audio]]></category>
		<category><![CDATA[codecs]]></category>
		<category><![CDATA[HTML 5]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[source]]></category>

		<guid isPermaLink="false">http://html5doctor.com/?p=497</guid>
		<description><![CDATA[Until very recently the ability to play any type of audio within a browser involved using Adobe Flash or other browser plugins. Although Adobe's Flash player is without doubt the most ubiquitous of these, most developers and designers would agree it is better not to rely on a plugin at all. Now thanks to HTML 5 and the browsers that implement its audio tag we can play audio natively within the browser.]]></description>
			<content:encoded><![CDATA[<p>Until recently, the ability to play any type of audio within a browser involved using Adobe Flash or other browser plugins. Although Adobe's Flash player is unquestionably the most ubiquitous of these, most developers and designers would agree that it's better not to rely on a plugin at all.</p>

<h2>Enter <abbr>HTML</abbr>5 <code>&lt;audio&gt;</code></h2>

<p>One of the most exciting and long-awaited features in <abbr>HTML</abbr>5 the <code>&lt;audio&gt;</code> element, enabling native audio playback within the browser. We can take advantage of this now as nearly all of the major browsers support it &mdash; currently Firefox, Chrome, Safari and Opera, but they are soon to be joined by Internet Explorer 9 which is currently in beta. For browsers that don't support audio natively, we can easily fallback to Flash.</p>

<h3>According to spec</h3>

<p>Currently, the <abbr>HTML</abbr>5 spec defines five attributes for the <code>&lt;audio&gt;</code> element:</p>

<ol>
<li><a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#attr-media-src"><code>src</code></a> &mdash; a valid &lt;abbr&gt;URL&lt;/abbr&gt; specifying the  content source</li>
<li><a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#attr-media-autoplay"><code>autoplay</code></a> &mdash; a boolean specifying whether the  file should play as soon as it can</li>
<li><a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#attr-media-loop"><code>loop</code></a> &mdash; a boolean specifying whether the file  should be repeatedly played.</li>
<li><a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#attr-media-controls"><code>controls</code></a> &mdash; a boolean specifying whether the  browser should display its default media controls</li>
<li><a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#attr-media-preload"><code>preload</code></a> &mdash; none / metadata / auto &mdash;  where 'metadata' means preload just the metadata and 'auto' leaves the  browser to decide whether to preload the whole file.</li>
</ol>

<p>Note preload supercedes autobuffer in the <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#attr-media-preload">latest  HTML5 spec</a>. Previously autobuffer took a boolean value specifying  whether the file is to be buffered in advance. Currently, browsers are making the transition from autobuffer to preload so we suggest that you use both attributes for the time being.</p>

<p>Incidentally these are the same attributes defined for the <code>&lt;video&gt;</code> element.</p>

<h3>Examples</h3>

<p>Let's take a couple of these attributes and create a simple example that will play an audio file:</p>

<pre><code>&lt;audio src="elvis.ogg" controls preload="auto" autobuffer&gt;&lt;/audio&gt;
</code></pre>

<p>(This example will work for the latest versions of Firefox, Chrome and Opera. You'll need to replace the Ogg file with an MP3 to get it working in Safari.)</p>

<p>Of course, the spec is not finalised, and there isn't yet a consensus on which codecs to support. This table details the codecs supported by today's browsers:</p>

<table>
  <caption>Codec support in modern desktop browsers</caption>
  <thead>
    <tr>
      <th>Browser</th>
      <th>Ogg Vorbis</th>
      <th>MP3</th>
      <th>WAV</th>
    </tr>
  </thead>
  <tbody>
    <td>FireFox 3.6+</td>
      <td>✓</td>
      <td></td>
      <td>✓</td>
    </tr>
    <tr>
      <td>Safari 5+</td>
      <td></td>
      <td>✓</td>
      <td>✓</td>
    </tr>
    <tr>
      <td>Chrome 6</td>
      <td>✓</td>
      <td>✓</td>
      <td></td>
    </tr>
    <tr>
      <td>Opera 10.5+</td>
      <td>✓</td>
      <td></td>
      <td>✓</td>
    </tr> 
    <tr>
      <td>Internet Explorer 9 (beta)</td>
      <td></td>
      <td>✓</td>
      <td>✓</td>
    </tr>
  </tbody>
</table>   

<p>To create our own controls, we can use the <abbr>API</abbr> methods defined by the spec:</p>

<ul>
<li><a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#dom-media-play"><code>play()</code></a> &mdash; plays the audio</li>
<li><a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#dom-media-pause"><code>pause()</code></a> &mdash; pauses the audio</li>
<li><a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#dom-navigator-canplaytype"><code>canPlayType()</code></a> &mdash; interrogates the browser to establish whether the given mime type can be played</li>
<li><a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#dom-media-buffered"><code>buffered()</code></a> &mdash; attribute that specifies the start and end time of the buffered part of the file</li>
</ul>

<h2>Use the Source</h2>

<p>The best way to coerce browsers into playing audio (or video, for that matter) is to use the <code>&lt;source&gt;</code> element. The browser will try to load the first audio source, and if it fails or isn't supported, it will move on to the next audio source. In addition, we can embed a Flash player if all else fails:</p></p>

<pre><code>&lt;audio controls preload="auto" autobuffer&gt; 
  &lt;source src="elvis.mp3" /&gt;
  &lt;source src="elvis.ogg" /&gt;
  &lt;!-- now include flash fall back --&gt;
&lt;/audio&gt;
</code></pre>

<p>One caveat, though: you may need to be careful about the order of the <code>&lt;source&gt;</code> elements. At the time of going to press issues have been reported with Mobile Safari and older versions of Firefox.</p>

<h2>Cross-Browser Implementation</h2>

<p>When we created <a href="http://happyworm.com/jquery/jplayer">jPlayer</a>, an audio player plugin for jQuery, we were attempting to address some of the limitations of the current crop of Flash-based audio players. Many relied on Flash to implement the player's graphical interface, effectively isolating the player from the rest of the web design process.</p>

<p>The original jPlayer relied on Flash to play the actual audio while allowing the look and feel to be styled via <abbr>HTML</abbr> and <abbr>CSS</abbr>. With growing support for <abbr>HTML</abbr>5 in modern browsers, we were inspired to break our Flash dependency and use native audio when it was supported.</p>

<p>The most significant issue is the cross-browser implementation, where lack of a common supported audio format among browsers causes complications. If developers want to take full advantage of all browsers that support <abbr>HTML</abbr>5 audio, they'll need to create both MP3 and Ogg (and in Opera's case, WAV) versions of the audio file they want to stream!</p>

<p>Since the <abbr>HTML</abbr>5 standard is still a work in progress, several aspects of the <code>&lt;audio&gt;</code> element vary from browser to browser. For example, there seems to be no way to determine the load progress of an audio file in all browsers as this relies on <a href="https://developer.mozilla.org/en/nsIDOMHTMLMediaElement"><code>buffered</code></a> DOM attribute being implemented. Chrome has implemented <a href="https://developer.mozilla.org/en/nsIDOMHTMLMediaElement"><code>buffered</code></a>, in Safari it's there but seems to behave slightly differently, current versions of Opera and Firefox however don't support it (<a href="http://hacks.mozilla.org/2010/08/html5-video-buffered-property-available-in-firefox-4/">although Firefox 4 beta does</a>).</p>

<p>Although these inconsistencies aren't showstoppers, in order to compete effectively with plugin-based solutions, we believe any <abbr>HTML</abbr>5 audio implementation should be consistent across all browsers and match current implementations feature for feature.</p>

<h3>JavaScript solutions</h3>

<p>If we intend to take advantage of each browser's audio capabilities, we need to create different solutions for different browsers. We could use browser sniffing, but considering the rapidly changing landscape, it's better to check what capabilities a particular browser supports and adapt accordingly.</p>

<p>To demonstrate this "feature sniffing", we've created a rough and ready <a href="http://www.happyworm.com/jquery/jplayer/HTML5.Audio.Support"><abbr>HTML</abbr>5 audio checker</a>.</p>

<p>Using JavaScript, you can check for audio tag support:</p>

<pre><code>// returns a boolean
var audioTagSupport = !!(document.createElement('audio').canPlayType);
</code></pre>

<p>or check file type compatibility:</p>

<pre><code>// Need to check the canPlayType first or an exception
    // will be thrown for those browsers that don't support it      

    var myAudio = document.createElement('audio'); 
    
    if (myAudio.canPlayType) {
      
       // Currently canPlayType(type) returns: "", "maybe" or "probably" 

       var canPlayMp3 = !!myAudio.canPlayType &#038;& "" != myAudio.canPlayType('audio/mpeg');
       var canPlayOgg = !!myAudio.canPlayType &#038;& "" != myAudio.canPlayType('audio/ogg; codecs="vorbis"');
    }</code></pre>

<p>Note that to change the <code>src</code> attribute of an audio object or element, you'll need to recreate the object or element with the new value for its <code>src</code> attribute or alternatively change the <code>src</code> URL and then issue a myAudio.load() command. </p>

<p>So, to create a solution that takes full advantage of <abbr>HTML</abbr>5 audio, you'll typically need to:</p>

<ol>
<li>check for <abbr>HTML</abbr>5 audio support, and if not present, fall back on Flash,</li>
<li>check the level of <abbr>HTML</abbr>5 audio support and adapt your code accordingly for each browser, and</li>
<li>check what file types are supported and link to appropriate formats of the files.</li>
</ol>

<h2>The Road Ahead</h2>

<p>Although <abbr>HTML</abbr>5 audio is a relatively immature part of the standard, if recent trends continue and users upgrade to the latest versions of Safari, Firefox, Chrome and Opera, browser support is likely above 30% today. This is a significant chunk of the browser market that will no longer need to rely on Adobe's Flash, Microsoft's Silverlight, or any other browser plugin for audio support. Add to this the fact that Internet Explorer 9 supports <abbr>HTML</abbr>5 audio and we could easily see the majority of installed browsers supporting it in the very near future.</p>

<p>And when you consider that mobile and other lower-spec devices such as tablets are choosing to support <abbr>HTML</abbr>5 audio, you begin to paint a picture of how important native audio support is becoming.</p>

<h2>Further reading:</h2>

<ul>
<li><p><a href="http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2009-June/020620.html">&#91;whatwg&#93; Codecs for <code>&lt;audio&gt;</code> and <code>&lt;video&gt;</code></a></p></li>
<li><p><a href="http://stackoverflow.com/questions/1007223/which-browsers-support-the-html-5-audio-tag-on-windows-today/1009015#1009015">Which browsers support <abbr>HTML</abbr>5 <code>&lt;audio&gt;</code> on Windows today?</a></p></li>
</ul>

<p><div id="crp_related"><h3>Related Posts:</h3><ul class="related"><li><a href="http://html5doctor.com/the-video-element/" rel="bookmark" class="crp_title">The video element</a></li><li><a href="http://html5doctor.com/video-the-track-element-and-webm-codec/" rel="bookmark" class="crp_title">Video: the track element and webM codec</a></li><li><a href="http://html5doctor.com/youtube-and-vimeo-support-html5-video/" rel="bookmark" class="crp_title">YouTube and Vimeo support HTML5 Video</a></li><li><a href="http://html5doctor.com/html5-briefing-notes-journalists-analysts/" rel="bookmark" class="crp_title">HTML5: briefing notes for journalists and analysts</a></li><li><a href="http://html5doctor.com/your-questions-answered-4/" rel="bookmark" class="crp_title">Your Questions Answered #4</a></li></ul></div></p>
<p><a href="http://html5doctor.com/native-audio-in-the-browser/" rel="bookmark">Native Audio in the browser</a> originally appeared on <a href="http://html5doctor.com">HTML5 Doctor</a> on July 29, 2009.</p>
]]></content:encoded>
			<wfw:commentRss>http://html5doctor.com/native-audio-in-the-browser/feed/</wfw:commentRss>
		<slash:comments>145</slash:comments>
		</item>
	</channel>
</rss>

