<?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>Learning jQuery &#187; HTML5</title>
	<atom:link href="http://www.learningjquery.org/index.php/tag/html5/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.learningjquery.org</link>
	<description>Learning jQuery: Tips, techniques, and tutorials for the jQuery JavaScript library</description>
	<lastBuildDate>Tue, 24 Aug 2010 10:10:35 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Introduction to the HTML5 audio tag javascript manipulation</title>
		<link>http://www.learningjquery.org/index.php/introduction-to-the-html5-audio-tag-javascript-manipulation/</link>
		<comments>http://www.learningjquery.org/index.php/introduction-to-the-html5-audio-tag-javascript-manipulation/#comments</comments>
		<pubDate>Thu, 16 Jul 2009 09:09:22 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.learningjquery.org/?p=52</guid>
		<description><![CDATA[by Cedric Dugas


Some of the new exciting features of HTML5 are the audio and video tag. This could potentially, in the long term, replace flash on the video level. I wanted to use the audio tag in one of my experiemental project, there is little information about how to manipulate the audio tag, and tought [...]]]></description>
			<content:encoded><![CDATA[<div class="date">by <a title="Posts by Cedric Dugas" href="http://www.position-absolute.com/author/karnius/">Cedric Dugas</a></p>
<div class="commentsNumber"></div>
</div>
<p>Some of the new exciting features of HTML5 are the audio and video tag. This could potentially, in the long term, replace flash on the video level. I wanted to use the audio tag in one of my experiemental project, there is little information about how to manipulate the audio tag, and tought it would be great to have a starter guide.</p>
<p>One thing clear is this is still an early implementation, which also means it is a bit buggy. I had a strange bug where if I had a song playing and had javascript throw errors at the same time (not related together), I could not terminate the song. Closing the tab would not stop it, I had to restart Firefox. Which lead me to think that the process is still not completely linked only to the tab (in Firefox at least).</p>
<p>I used jquery in my demo because the application I developing is wrapped around it, but this is not necessary.</p>
<p class="clearfix"><a class="demo" href="http://www.position-relative.net/creation/audiotag/" target="_blank">View demo</a><br />
(Only consisting of 5 buttons)</p>
<h3>What I learned</h3>
<p>The API is somewhat easy to work with, as it should be, play(), pause(), want to stop the volume? audioElement.volume=0. Want to start playing at 55 seconds? audioElement.currentTime = 55. Pretty cool stuff.</p>
<p>The default html audio player controls are added automatically with a simple: controls=”true”, it has good and bad sides. You don’t have to worry about it, but at the same time, for now, you cannot modify the player style. Also when you right click you have a nice contextual menu where you can save the file in ff3.5.</p>
<p><strong>A normal audio tag</strong></p>
<p><strong>Your browser does not support the audio tag.</strong></p>
<p><strong>Add and play a sound via JavaScript</strong></p>
<div class="geshi no html">
<ol>
<li class="li1">
<div class="de1">var audioElement = document.createElement(&#8216;audio&#8217;);</div>
</li>
<li class="li1">
<div class="de1">audioElement.setAttribute(&#8217;src&#8217;, &#8216;loading.ogg&#8217;);</div>
</li>
<li class="li1">
<div class="de1">audioElement.play();</div>
</li>
</ol>
</div>
<p><strong>Get the song filepath and duration</strong></p>
<div class="geshi no html">
<ol>
<li class="li1">
<div class="de1">audioElement.src;</div>
</li>
<li class="li1">
<div class="de1">audioElement.duration;</div>
</li>
</ol>
</div>
<p><strong>Load a sound</strong></p>
<div class="geshi no html">
<ol>
<li class="li1">
<div class="de1">var audioElement = document.createElement(&#8216;audio&#8217;);</div>
</li>
<li class="li1">
<div class="de1">audioElement.setAttribute(&#8217;src&#8217;, &#8216;Mogwai2009-04-29_acidjack_t16.ogg&#8217;);</div>
</li>
<li class="li1">
<div class="de1">audioElement.load()</div>
</li>
<li class="li1">
<div class="de1">audioElement.addEventListener(&#8220;load&#8221;, function() {</div>
</li>
<li class="li1">
<div class="de1">audioElement.play();</div>
</li>
<li class="li1">
<div class="de1">$(&#8220;.duration span&#8221;).html(audioElement.duration);</div>
</li>
<li class="li1">
<div class="de1">$(&#8220;.filename span&#8221;).html(audioElement.src);</div>
</li>
<li class="li1">
<div class="de1">}, true);</div>
</li>
</ol>
</div>
<p><strong>Stop a song</strong></p>
<div class="geshi no html">
<ol>
<li class="li1">
<div class="de1">audioElement.pause();</div>
</li>
</ol>
</div>
<p><strong>Change volume</strong></p>
<div class="geshi no html">
<ol>
<li class="li1">
<div class="de1">audioElement.volume=0;</div>
</li>
</ol>
</div>
<p><strong>Play at exactly 35 seconds in the song</strong></p>
<div class="geshi no html">
<ol>
<li class="li1">
<div class="de1">audioElement.currentTime=35;</div>
</li>
<li class="li1">
<div class="de1">audioElement.play();</div>
</li>
</ol>
</div>
<h3>The bad parts</h3>
<p>No stop(), only pause(), I really do not know what stopped them to put stop() in the API.<br />
No way to get the songs name.<br />
The only accessible format are ogg and wav, no mp3 love. It also means that you will have to convert all your sounds in ogg.<br />
As I said this is a bit buggy but understandable, the specs are not even finished.<br />
Microsoft will probably implement the audio and video tag in 2022.</p>
<h3>The good parts</h3>
<p>The API is easy to use, you can have a song playing in no time.<br />
You do not need to parse the audio object in the HTML body, you can have it played directly in your script.<br />
In the end, I’m really happy on how this turned out.</p>
<p class="clearfix"><a class="download" href="http://www.position-relative.net/creation/audiotag/audiotag.zip">Download the source code</a> <a class="demo" href="http://www.position-relative.net/creation/audiotag/" target="_blank">View demo</a></p>
<p><strong>Tested on:</strong><br />
Firefox 3.5</p>
<h3>Ressources</h3>
<p><a href="http://hacks.mozilla.org/2009/06/exploring-music-audio/">http://hacks.mozilla.org/2009/06/exploring-music-audio/</a><br />
<a href="https://developer.mozilla.org/en/Using_audio_and_video_in_Firefox">https://developer.mozilla.org/en/Using_audio_and_video_in_Firefox</a><br />
<a href="http://webkit.org/blog/140/html5-media-support/">http://webkit.org/blog/140/html5-media-support/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.org/index.php/introduction-to-the-html5-audio-tag-javascript-manipulation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
