Script for Simple HTML5 Audio Player using Javascript
HTML5 enables developers to embed audio in HTML file and build simple HTML5 audio player using new HTML5 audio element with Javascript. Scripting audio is has never been so easy using Javascript and HTML5 new features.
Just like the case in playing video files in HTML, web programmers want to create custom HTML5 audio player controls like to play, stop (pause) or rewind audio or music file. In this case while you create visual items instead of HTML5 audio controls, by scripting audio elements using Javascript developers can enable web users to control audio element in HTML page by their own custom way.
To embed audio in HTML page and programming audio controls is possible by creating custom audio player and scripting audio using Javascript in HTML page. In this HTML5 tutorial I want to show how HTML developers can script HTML5 audio element using Javascript programming.
As a first step in building simple HTML5 audio player, I prefer to add new HTML5 tag audio with controls disabled.
So web users will not be able to realize that there is an HTML5 audio element in the HTML page.
All you have to do is copy and paste on your HTML5 web page source code.
As the second step in this tutorial, make a list of songs that the visitor can choose from for listening music on your web page. I used the songs that exists by default in Sample Music directory of my Windows 7 installation. Kalimba, Maid with the Flaxen Hair and Sleep Away.
For each song in the list, I defined an onClick event which triggers Javascript function SetHTML5AudioFile. Within this Javascript function, the HTML5 audio element source object is set using src property of the audio object.
In order to simplify the below code please concantrate on:
var audio = document.getElementById("audio");
where HTML5 audio element in HTML page is fetched using getElementById, and
audio.src = audio_file_path;
where audio source file (music or sound file) URL path is set.
In the next step in this HTML5 audio tutorial you can now create three controls for building a simple HTML5 audio player. These controls are Play, Pause and Rewind buttons for corresponding functions in embedded HTML5 audio.
Of course you can create fantastic images and layout for your custom HTML5 audio player. I simply added three buttons in HTML5 page.
What is important about these HTML buttons are they trigger custom Javascript functions defined in onClick event of each button.
For example, btnPlay triggers PlayAudioFile() Javascript function.
Here is the source of the PlayAudioFile() function.
First audio element in HTML5 page is fetched by using document.getElementById.
Then audio object whose source is previously defined is started to play using audio element play() method.
As similar to Play control, Pause control also first gets the audio element created in HTML5 page. Then pause() method of the audio object is called as follows:
Rewind control in our simple player is not different from above. First HTML5 programmers should get the audio object defined using <audio> tag within the HTML page.
But then using audio object currentTime property, developers can set the current position of the audio file to the beginning by setting "currentTime = 0"
Here is the Javascript code which can be used for scripting HTML5 audio object for rewind.
Developers can also adjust volume by programming audio element in HTML5 page.
Please check a working sample for simple HTML5 audio player described in this HTML5 tutorial at Scripting HTML5 Audio Element using Javascript. And here is the HTML source for for this HTML5 tutorial demonstrating HTML5 audio element and scripting embedded audio in HTML file.