html js 控制点击一次按钮就播放一次音频,怎么弄,

JavaScript016

html js 控制点击一次按钮就播放一次音频,怎么弄,,第1张

<script>    

<!--\\    

if(navigator.userAgent.indexOf("MSIE")!=-1)    

{    

       document.write('<OBJECT classid="clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6" width="0" height="0" id="player">')    

       document.write('<param name="AutoStart" value="true"/>') //控制声音是否自动播放    

document.write('<param NAME="PlayCount" VALUE="99">')    

       document.write('<param name="URL" value="/templets/cloth/music/music.mp3"/>')    

       document.write('</OBJECT>')    

}    

else    

{    

       document.write('<OBJECT type="application/x-ms-wmp" autostart="true" src="/templets/cloth/music/music.mp3" width="0" height="0" id="player">')    

document.write('<param NAME="PlayCount" VALUE="99">')    

document.write('</OBJECT>')    

}    

$(function(){    

$("#music_button").toggle(function(){    

document.all.player.controls.stop()    

$(this).attr("src","/templets/cloth/images/music.jpg")    

},function(){    

document.all.player.controls.play()    

$(this).attr("src","/templets/cloth/images/music.gif")    

})    

})    

//-->    

</script>

这里我们可以使用JS来进行控制,代码如下:

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) + ':秒')

}

function playOrPaused(id,obj){

if(audio.paused){

audio.play()

obj.innerHTML='暂停'

return

}

audio.pause()

obj.innerHTML='播放'

}

function hideOrShowControls(id,obj){

if(audio.controls){

audio.removeAttribute('controls')

obj.innerHTML = '显示控制框'

return

}

audio.controls = 'controls'

obj.innerHTML = '隐藏控制框'

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 = '开启静音'

}else{

audio.muted = true

obj.innerHTML = '关闭静音'

}

}

//保留一位小数点

function returnFloat1(value) {

value = Math.round(parseFloat(value) * 10) / 10

if (value.toString().indexOf(".") <0){

value = value.toString() + ".0"

}

return value