The <video> and <audio> elements introduced in HTML5 made your media assets really available to the Internet. HTML5 videos are quickly replacing Flash Player and other third-party media players. Getting media to play correctly has always been a time-consuming task. To make the media playback operate, the <embed> and <object> tags are frequently utilized, which assign a long set of parameters.
HTML5 <audio> Element:
The HTML5 <audio> tag can be used to add HTML music code to your website. This element was initially introduced in HTML5, which was still under development at the time of writing, so make careful to test your music code in multiple browsers. Although most major browsers support the <audio> element, compatibility for different file types varies.
Simple Html5 Audio:
The new audio element is the simplest method to include audio into a web page using HTML5. Use the following code to include this in your HTML5 document:
- <audio controls=”controls”>
- <source src=”track.ogg” type=”audio/ogg” />
- <source src=”track.mp3″ type=”audio/mpeg” />
- Your browser does not support the audio element.
If you look at the code above, you’ll notice that I’ve created the audio> tag and specified the controls property, allowing us to access the player’s default controls.
Two’src’ tags are nestled within the ‘audio>’ tag. The first specifies an MP3 track, whereas the second specifies the OGG format. The OGG format is specifically used to allow music to play in Firefox, as Firefox does not support MP3 without the need of a plugin owing to licensing difficulties.A paragraph of text the audio element is not supported by your browser. It lets people with browsers that don’t support it know what’s going on.
Html5 Audio Tag Attributes:
In addition to supporting global HTML5 properties, the tag also has its own set of attributes.
- Auto play: This may be adjusted to “true” or left blank “” to control whether the track starts playing as soon as the page loads.
- Controls: As shown in the sample above, this determines whether native controls like ‘play, pause,’ and so on should be shown.
- Loop: When set to “loop,” this determines whether or not the track will play again after it has completed.
- Preload – This can be set to “auto” (which means the file should load as soon as the page loads), “metadata” (which means only the metadata, track title, and so on should be loaded), or “none” (which means the file should not be loaded at all) (which dictates that the browser should not load the file when the page loads).
- src – As seen in the previous example, this specifies the URL of the music to be played by the audio tag.
Turning it all the way up to eleven:
var myaudio = new Audio(‘mysong.mp3’);
That’s all there is to it! Then, if we want to do something with the audio, we can use the variable ‘myaudio’ to activate it. Here’s a list of possible actions using the variable. Please make a list of them; we’ll need some later in the tutorial when we build our audio player.
myaudio.play(); – This will play the music.
myaudio.pause(); – This will stop the music.
myaudio.duration; – Returns the length of the music track.
myaudio.currentTime = 0; – This will rewind the audio to the beginning.
myaudio.loop = true; – This will make the audio track loop.
myaudio.muted = true; – This will mute the track
Markup for creating the HTML5 Audio Player:
Now that you’ve learned a little about HTML5 audio and grasped the fundamental ideas, it’s time to put what you’ve learned into practice and design your HTML5 audio player. I’m skipping the design step because it’s outside the tutorial scope, but you can download the source code and go to the PSD to get a sense of how it’s put together.
- <!DOCTYPE html>
- <title>HTML5 Audio Tutorial</title>
- <link rel=”stylesheet” type=”text/css” href=”http://yui.yahooapis.com/3.3.0/build/cssreset/reset-min.css”>
- <link href=’http://fonts.googleapis.com/css?family=Lobster’ rel=’stylesheet’ type=’text/css’>
- <link rel=”stylesheet” type=”text/css” href=”css/style.css”>
After the h1 title, you can create a div with a class of “container” and “gradient”. You can also create a separate class of gradient as this will be reused on some other elements. Within “.container” you will add an image (which will be the album cover), then the three anchor tags will act as the controls for the player. Between them you’ll find the scrubber/HTML5 range input field.
How to preload a sound file:
You can preload music files so that they start playing in the background before the user presses the “Play” button. Preloading can be done in the following way:
You may use the preload property on the <audio> element. None, metadata, and auto are examples of possible values. The audio/music should begin downloading in the background as soon as the page has loaded if auto is selected. It will only download pertinent metadata, such as the initial keyframe, duration, and size, if metadata is specified.
Online Available Html Players:
You can successfully share your audio files in a visually beautiful and engaging way by installing a premium HTML5 audio player plugin to your website.
- The Best HTML5 Audio Player Plugins on CodeCanyon
Explore CodeCanyon’s vast collection of the highest-quality HTML5 audio player scripts and plugins available. You may buy one of these scripts or plugins for a nominal one-time fee and increase traffic to your website.
On CodeCanyon, here are a handful of the most popular HTML5 audio player plugins.
Paid Html Music Players:
- AudioBox – HTML5 Music Player
- sPlayer – Super HTML5 Music Player
- Responsive HTML5 Music Player for WordPress
- HTML5 Music Player with Playlist
- SPlayer: Supercharged HTML5 Music Player
Free Html Players:
- Media Element
- SoundManger 2
- Html audio player