Working with Audio in Html5

About Working with Audio in Html5

AwesomeCo is developing a site to showcase some royalty-free audio loops for use in screencasts, and it would like to see a demo page mocked up of a single loop collection to Working with Audio in Html5. When we’re done, we’ll have a list of the audio loops, and a visitor will be able to quickly audition each one. W e don’t have to worry about finding audio loops for this project, because the client’s sound engineer has already provided us with the samples we’ll need in both MP3 and OGGf ormats. You canfi nd as mall bit of information on how to encode your own audio files in Appendix, The audio engineer has provided us with four samples: drums, organ,bass, and guitar. W e need to describe each one of these samples using HTML markup. Here’s the markup for the drums loop:

<article class="sample">
<header><h2>Drums</h2></header>
<audio id="drums" controls>
<source src="sounds/ogg/drums.ogg" type="audio/ogg">
<source src="sounds/mp3/drums.mp3" type="audio/mpeg">
<a href="sounds/mp3/drums.mp3">Download drums.mp3</a>
</audio>
</article>

We define the audio element first and tell it that we want to have some controls displayed. Next, we define multiple sources for the file. We first define the MP3 and OGG versions of the sample, and then we display a link to allow the visitor to download the MP3 file directly if the browser doesn’t support the audio element.This very basic bit of code will work in Chrome, Safari, and Firefox. Let’s put it inside an HTML5 template with the three other sound samples.

<article class="sample">
<header><h2>Drums</h2></header>
<audio id="drums" controls>
<source src="sounds/ogg/drums.ogg" type="audio/ogg">
<source src="sounds/mp3/drums.mp3" type="audio/mpeg">
<a href="sounds/mp3/drums.mp3">Download drums.mp3</a>
</audio>
</article>
<article class="sample">
<header><h2>Guitar</h2></header>
<audio id="guitar" controls>
<source src="sounds/ogg/guitar.ogg" type="audio/ogg">
<source src="sounds/mp3/guitar.mp3" type="audio/mpeg">
<a href="sounds/mp3/guitar.mp3"> Download guitar.mp3</a>
</audio>
</article>
<article class="sample">
<header><h2>Organ</h2></header>
<audio id="organ" controls>
<source src="sounds/ogg/organ.ogg" type="audio/ogg">
<source src="sounds/mp3/organ.mp3" type="audio/mpeg">
<a href="sounds/mp3/organ.mp3">Download organ.mp3</a>
</audio>
</article>
<article class="sample">
<header><h2>Bass</h2></header>
<audio id="bass" controls>
<source src="sounds/ogg/bass.ogg" type="audio/ogg">
<source src="sounds/mp3/bass.mp3" type="audio/mpeg">
<a href="sounds/mp3/bass.mp3"> Download bass.mp3</a>
</audio>
</article>
</body>
</html>

Falling Back for Working with Audio in Html5

Audio fallback support is built into the element itself. We’ve defined multiple sources for our audio using the source element and have provided links to download the audio files. If the browser cannot render the audio element, it will display the link we’ve placed inside the field. We could even go a step further and use Flash as a fallback after we AwesomeCo is developing a site to showcase some royalty-free audio loops for use in screencasts, and it would like to see a demo page mocked up of a single loop collection to Working with Audio ,
define our sources.When we open the page in an HTML 5-compatible browser, each entry in the list will have its own audio player,on the following page. The browser itself handles the playback of the audio when you press the Play button. When we open the page in Internet Explorer, the download links show since the browser doesn’t understand the audio element. This makes for a decent fallback solution, but let’s see whether we can do better.

Working with Audio in Html5

Working with Audio in Html5

best possible approach To Working with Audio in Html5

However, this might not be the best possible approach to AwesomeCo is developing a site to showcase some royalty-free audio loops for use in screencasts, and it would like to see a demo page mocked up of a single loop collection to Working with Audio in Html5. You may encounter a browser that supports the  Working with Audioelement but doesn’t support the formats you’ve supplied. For example, you may decide it’s not worth your time to provide audio in multiple formats. Additionally, the HTML 5 specification specifically mentions that the fallback support for  Working with Audio is not to be used to place content that would be read by screen readers. The simplest solution is to move the download link outside the audio element and use JavaScript to hide it, like this is use For Working with Audio in Html5:

article class="sample">
<header><h2>Drums</h2></header>
<audio id="drums" controls>
<source src="sounds/ogg/drums.ogg" type="audio/ogg">
<source src="sounds/mp3/drums.mp3" type="audio/mpeg">
</audio>
<a href="sounds/mp3/drums.mp3">Download drums.mp3</a>
</article>

Then we just need to detect support for AwesomeCo is developing a site to showcase some royalty-free audio loops for use in screen casts, and it would like to see a demo page mocked up of a single loop collection to Working with Audio in Html5 and hide the links. We do that by creating a new audio element in JavaScript and checking to see whether it responds to the canPlayType() method, like this:

var canPlayAudioFiles = !!(document.createElement( 'audio' ).canPlayType);

We evaluate the response and then hide any anchors that are nested within our sample sections For Working with Audio in Html5.

$(function(){
var canPlayAudioFiles = !!(document.createElement( 'audio' ).canPlayType);
if(canPlayAudioFiles){
$(".sample a" ).hide();
};
});

Fall backs to Working with Audio are relatively easy, and some of your users may actually appreciate the ability to easily download the file. Playing audio in the browser natively is just the beginning. Browsers are just starting to support the HTML 5 JavaScript APIs for audio and video, which you can read about in the sidebar to Working with Audio in Html5.

andcombd