HTML5 audio labels use js for playback control instances, html5js

Source: Internet
Author: User

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>

Related Article

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.