HTML5 audio labels use js for playback control instances, html5js
This article mainly introduces the HTML5 audio 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, for more information, see
The <audio> tag can play audio files in the HTML5 browser.
<Audio> a control panel is provided by default, but sometimes we only need to play the sound. We 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:
<A href = "javascript: void (0);" onclick = "getCurrentTime ('Firefox ');"> Get playback time </a>
<A href = "javascript: void (0);" onclick = "playOrPaused ('Firefox ', this);"> play </a>
<A href = "javascript: void (0);" onclick = "hideOrShowControls ('Firefox ', this);"> hide the control box </a>
<A href = "javascript: void (0);" onclick = "muted ('Firefox ', this);"> enable mute </a>
<Input type = "button" value = "+" id = "upVol" onclick = "vol ('Firefox ', 'up', this) "/> volume <input type =" button "value ="-"onclick =" vol ('Firefox ', 'lowdown', this) "/>
<Audio src = "/images/audio/Never Say Good Bye.ogg" id = "audio" controls = "controls"> </audio>
Current volume: <span id = "nowVol">-</span>