Step 3: Button controls
1.Play/ Pause
As the name suggests this button is to play or pause the audio file.
<button id=”play” onClick=”playAudio(); “>Play/Pause</button>
The play and pause methods are used to provide playback control. The button object is retrieved so that the button label can be toggled between Play and Pause, depending on the state of the audio object’s paused property. The state is checked every time the “playAudio” function is called. If the audio file is playing, the paused property returns false, and the pause method is called to pause the playback. The button label is also set to Play.
If the file is paused, the paused property returns true, and the play method is called, the button label is updated to Pause. When a file is first loaded, the paused property returns true (playback is paused) even though the pause method has not explicitly been called.
In the JavaScript portion, using document.getElementById, the audio object is returned in oAudio, the button object is returned in btn and list object is returned in audioURL. To play more than one file, you can set the audio object’s src property to a URL of an audio file from within JavaScript.
The global variable “currentFile” is defined so that it keeps track of the URL for the file that is currently playing. When the user clicks the Play button, the “currentFile” variable is compared to the value in the text field that is specified by “audioURL.value”. If these values are different, the src property is set to the new file URL, the “currentFile” variable is updated, and the load method is called.
var currentFile = “”; //Global variable to track current file
function playAudio() {
// Check for audio element support.
if (window.HTMLAudioElement) {
try {
var oAudio = document.getElementById(‘myaudio’);
var btn = document.getElementById(‘play’);
var audioURL = document.getElementById(‘mylist’); //Skip loading if current file hasn’t changed.
if (audioURL.value !== currentFile) {
oAudio.src = audioURL.value;
currentFile = audioURL.value;
}// Tests the paused attribute and set state.
if (oAudio.paused) {
oAudio.play();
btn.textContent = “Pause”;
}
else {
oAudio.pause();
btn.textContent = “Play”;
}
}
catch (e) {
// Fail silently but show in F12 developer tools console
if(window.console && console.error(“Error:” + e));
}
}
}
In the JavaScript section of the code, there are areas where errors are likely. The first is when you check for HTML5 audio support. Each function tests by using if (window.HTMLAudioElement) to see if the audio element exists. If the audio element does not exist no code is executed.
If HTML5 audio is supported, there are other errors that might happen. Try/catch statements are used in conjunction with methods that can throw exceptions. One cause of exceptions is if the user tries to play a file that does not exist, rewind when no file is loaded, or a connection to a file cannot be made.
With the try/catch statements, these conditions fail silently, but you can see the errors if you open either the Console or Script tab in Internet Explorer 9 F12 tools. For example, the Fast Forward, Rewind, and Restart functions throw “InvalidStateError” DOMExceptions if there is no audio file playing or loaded.