The
tag can play audio files in HTML5 browsers.
A control panel is provided by default, but sometimes we only need to play sounds, and the control panel is up to us to define its displayed status.
Here we can use JS to control, 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) ': seconds'); } 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 = 'Show 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 = 'Turn off mute'; } } //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 calling method is as follows: