The HTML5 audio tag uses JS for playback control instances

The < audio > tag can play audio files in an HTML5 browser.

< audio > provides a control panel by default, but sometimes we only need to play sound. The display status of the control panel is defined by ourselves.

Here we can use js for control. The code is as follows:

Copy code

The code is as follows:

var audio ;
window.onload = function(){
initAudio();
}
var initAudio = function(){
//audio = document.createElement(“audio”)
//audio.src=’Never Say Good Bye.ogg’
audio = document.getElementById(‘audio’);
}
function getCurrentTime(id){
Alert (parseInt (audio. Currenttime) + ‘: second’);
}

function playOrPaused(id,obj){
if(audio.paused){
audio.play();
obj. InnerHTML = ‘pause’;
return;
}
audio.pause();
obj. InnerHTML = ‘play’;
}

function hideOrShowControls(id,obj){
if(audio.controls){
audio.removeAttribute(‘controls’);
obj. InnerHTML = ‘display control box’
return;
}
audio.controls = ‘controls’;
obj. InnerHTML = ‘hide control box’
return;
}
function vol(id,type , obj){
if(type == ‘up’){
var volume = audio.volume + 0.1;
if(volume >=1 ){
volume = 1 ;

}
audio.volume = volume;
}else if(type == ‘down’){
var volume = audio.volume – 0.1;
if(volume <=0 ){
volume = 0 ;
}
audio.volume = volume;
}
document.getElementById(‘nowVol’).innerHTML = returnFloat1(audio.volume);
}
function muted(id,obj){
if(audio.muted){
audio.muted = false;
obj. InnerHTML = ‘turn on mute’;
}else{
audio.muted = true;
obj. InnerHTML = ‘mute off’;
}
}
//Keep one decimal point

function returnFloat1(value) {
value = Math.round(parseFloat(value) * 10) / 10;
if (value.toString().indexOf(“.”) < 0){
value = value.toString() + “.0”;
}
return value;
}

The calling method is as follows:

Copy code

The code is as follows:

Get playback time
Play
Hide control frame
Turn on mute
volume

Current volume: < span id = “nowvol” > -</span>

Read more here: Source link