This article mainly introduces the HTML5audio tag using js for playback control instances. This article provides code examples to demonstrate how to obtain the playback time, play, pause, mute, and other control methods, you can refer to the following tags to play audio files in the HTML5 browser.
A control panel is provided by default, but sometimes we only need to play the sound. We can define the display status of the control panel by ourselves.
Here we can use JS for control. The Code is as follows:
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 = 'stopped ';
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 = 'low '){
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 = 'Enable muting ';
} Else {
Audio. muted = true;
Obj. innerHTML = 'Disable muting ';
}
}
// Retain 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 call method is as follows:
The Code is as follows:
Obtain the playback time
Play
Hide Control Box
Enable mute
Volume
Current volume :-