HTML5audio labels use JavaScript For playback control instances _ html5 tutorial tips-

Source: Internet
Author: User
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 :-

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.