audio自定義樣式,控制操作面板的暫停,播放,獲取音訊的時長,以及根據時長進行進度條展示
阿新 • • 發佈:2018-11-09
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>audio例項</title>
<script src="./js/jquery-1.9.1.min.js"></script>
</head>
<body>
<div class="btn-audio"><audio id="mp3Btn"><source src="./music1.mp3" type ="audio/mpeg" /></audio></div>
<div class="progress">
<!--進度條-->
<div class="loader-container"></div>
<div class="controls">
<span class="played-time">00:00</span>
<span class="audio-time" id="audioTime" >0</span>
</div>
</div>
</body>
</html>
<script type="text/javascript">
$(function(){
//播放完畢
$('#mp3Btn').on('ended', function() {
console.log("音訊已播放完成");
$('.loader-container').css('width','100%');
$('.btn-audio' ).css({'background':'url(images/voice_stop.png) no-repeat center bottom','background-size':'cover'});
})
//播放器控制
var audio = document.getElementById('mp3Btn');
audio.volume = .100;
$('.btn-audio').click(function() {
event.stopPropagation();//防止冒泡:阻止目標元素的事件冒泡到父級元素
if(audio.paused){ //如果當前是暫停狀態
$('.btn-audio').css({'background':'url(images/voice_play.png) no-repeat center bottom','background-size':'cover'});
audio.play(); //播放
return;
}else{//當前是播放狀態
$('.btn-audio').css({'background':'url(images/voice_stop.png) no-repeat center bottom','background-size':'cover'});
audio.pause(); //暫停
}
});
//獲取時長
//loadedmetadata事件為音訊/視訊檔案載入完資料後觸發
// duration 獲取音訊的時長,單位為s
// transTime為封裝好的一個函式,目的是將秒轉換為幾分幾秒的格式
$('#mp3Btn').on("loadedmetadata",function () {
// alert(audio.duration)
$('#audioTime').text(transTime(this.duration));
});
//監聽音訊播放時間並更新進度條
audio.addEventListener('timeupdate',updateProgress,false);
})
//轉換音訊時長分/秒顯示
function transTime(time) {
var duration = parseInt(time);
var minute = parseInt(duration/60);
var sec = duration%60+'';
var isM0 = ':';
if(minute == 0){
minute = '00';
}else if(minute < 10 ){
minute = '0'+minute;
}
if(sec.length == 1){
sec = '0'+sec;
}
return minute+isM0+sec
}
//更新進度條
function updateProgress() {
var audio =document.getElementsByTagName('audio')[0]; //js獲取的方式
var value = Math.round((Math.floor(audio.currentTime) / Math.floor(audio.duration)) * 100);//當前時間/總長 再乘以一個100變成百分數
$('.loader-container').css('width', value * 0.907 + '%');//0.907 :進度條div的寬度除以背景img的寬度,如果他們倆的長度一樣,就直接value就可以。
$('.played-time').html(transTime(audio.currentTime));
}
</script>
<style type="text/css">
html,body{
margin: 0;
height: 100%;
width: 100%;
overflow: hidden;
background:#2b2938;
}
.btn-audio{
margin: 90px auto;
width: 186px;
height: 186px;
background:url(images/voice_stop.png) no-repeat center bottom;
background-size:cover;
}
.loader-container {
height: 100%;
/*進度條長度*/
width: 0;
background: -webkit-linear-gradient(left,#f8dc4b,#d95a3d);
background: -moz-linear-gradient(left,#f8dc4b,#d95a3d);
background: -o-linear-gradient(left,#f8dc4b,#d95a3d);
background: -ms-linear-gradient(left,#f8dc4b,#d95a3d);
background: linear-gradient(left,#f8dc4b,#d95a3d);
border-radius:7px;
box-shadow: inset 0 -2px 2px rgba(0, 0, 0, 0.2);
animation: loader 1s linear;
}
.progress{
width: 70%;
height: 7px;
border-radius:7px;
margin-left: 15%;
margin-top: 30%;
background: -webkit-linear-gradient(left,#e4e3e4,#e4e5e4);
background: -moz-linear-gradient(left,#e4e3e4,#e4e5e4);
background: -o-linear-gradient(left,#e4e3e4,#e4e5e4);
background: -ms-linear-gradient(left,#e4e3e4,#e4e5e4);
background: linear-gradient(left,#e4e3e4,#e4e5e4);
}
.controls span{color: #b3b5b7; font-size: 12px; display: inline-block; width: 34px;}
</style>