Skip to content

Commit

Permalink
优化2
Browse files Browse the repository at this point in the history
  • Loading branch information
small-Black committed Oct 19, 2015
1 parent 828fedb commit 978780e
Show file tree
Hide file tree
Showing 3 changed files with 230 additions and 187 deletions.
131 changes: 131 additions & 0 deletions Audio/AudioPlay.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
(function() {
function AudioPlay(options) {
//输入
this.audio = document.querySelector(options.dom) || null; //audio元素
this.data = options.data || []; //音频数据
this.autoplay = options.autoplay || false; //自动播放

// 获取DOM节点
this.music_name = document.querySelector(".audioplay-name") || null;
this.control = document.querySelector(".audioplay-control") || null;
this.control_status = document.querySelector("#audioplay-status") || null;
this.total_time_dom = document.querySelector(".audioplay-total-time") || null;
this.cur_time_dom = document.querySelector(".audioplay-cur-time") || null;
this.list = document.querySelector(".audioplay-list ul") || null;
this.progress_status = document.querySelector(".audioplay-progress-status") || null;
this.progress = document.querySelector(".audioplay-progress");
this.volume = document.querySelector(".audioplay-volume");
this.volume_status = document.querySelector(".audioplay-volume-status");
this.progress_buffered = document.querySelector(".audioplay-progress-buffered") || null;

this.init();


}
//初始化
AudioPlay.prototype.init = function() {

var data = this.data;
if (data.length > 0) {
//初始化播放列表
for (var i = 0, len = data.length; i < len; i++) {
this.list.innerHTML += "<li data-url=" + data[i].url + " class=" + (i === 0 && "on") + " >" + data[i].name + "</li>";
}
this.music_name.innerHTML = data[0].name;
this.audio.src = data[0].url;
}

this.listener();
};
//添加监听事件
AudioPlay.prototype.listener = function() {
var list_li = document.querySelectorAll(".audioplay-list ul li") || null,
self = this,
audio = self.audio, //
total_width = self.progress.clientWidth, //进度条的总宽度
volume_width = self.volume.clientWidth; //音量条总宽度。
//加载完成,设置歌曲总时长
audio.addEventListener("loadedmetadata", function() {
self.total_time_dom.innerHTML = parseInt(audio.duration / 60) + ":" + parseInt(audio.duration % 60);
});
//播放暂停控制
self.control.addEventListener("click", function() {
self.playOrPause();
});
//播放完毕
audio.addEventListener("ended", function() {
self.reset();
});
//时间改变,当前时间与进度条更新
audio.addEventListener("timeupdate", function() {
if (!isNaN(audio.duration)) {
self.cur_time_dom.innerHTML = parseInt(audio.currentTime / 60) + ":" + parseInt(audio.currentTime % 60);
self.progress_status.style.width = parseInt(audio.currentTime / audio.duration * total_width) + "px";
}
});
//歌典加载过程中,更新缓冲进度条。
audio.addEventListener("progress", function() {
var buffered = audio.buffered.length;
if (buffered > 0) {
self.progress_buffered.style.width = audio.buffered.end(buffered - 1) / audio.duration * total_width + "px";
}
});
//音量控制
self.volume.addEventListener("click", function(e) {

audio.volume = (e.offsetX / volume_width) > 1 ? 1 : (e.offsetX / volume_width);
self.volume_status.style.width = e.offsetX + "px";

});
//进度条控制
self.progress.addEventListener("click", function(e) {
audio.currentTime  = parseInt((e.offsetX / total_width) * audio.duration);
self.progress_status.style.width = e.offsetX + "px";
});
//播放列表点击
Array.prototype.map.call(list_li, function(dom, index) {
dom.addEventListener("click", function() {
//设置选中状态
Array.prototype.map.call(list_li, function(dom) {
dom.className = "";
});
dom.className = "on";
var dataset = dom.dataset;
//重新加载歌曲
self.music_name.innerHTML = dom.innerText;
audio.src = dataset.url;
audio.load();
self.reset();
self.playOrPause();
});
});
};
//重置歌曲状态。
AudioPlay.prototype.reset = function() {
this.progress_status.style.width = 0;
this.control_status.className = "audioplay-playing";
};
//播放或者暂停
AudioPlay.prototype.playOrPause = function() {
if (this.audio.paused) {
this.audio.play();
console.log(this.audio.duration);
this.control_status.className = "audioplay-pauseing";
} else {
this.audio.pause();
this.control_status.className = "audioplay-playing";
}
};

new AudioPlay({
dom: "audio",
autoplay: true,
data: [{
name: "春秋配",
url: "春秋配.mp3"
}, {
name: "富士山下",
url: "富士山下.mp3"
}]
});
})();
145 changes: 94 additions & 51 deletions Audio/audio.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,43 @@
font-size: 16px;
}

ul,
li {
list-style: none;
}

.clearfix {
overflow: hidden;
_zoom: 1;
}

.audioplay {
width: 100%;
background: #EFEFEF;
border: 1px solid #000;
padding: 5%;
font-family: "Microsoft YaHei";
width: 90%;
margin: 10px auto;
/* background: #EFEFEF;*/
border: 1px solid #ccc;
border-radius: 5px;
padding: 10px 2%;
background: linear-gradient(#fff, #EFEFEF);
/* 标准的语法 */
}

.audioplay-name {
text-align: center;
}


.audioplay-box > div {
float: left;
}

.audioplay-control {
width: 50px;
padding: 10px;
background: #D0CECF;
width: 12%;
padding: 5px;
border: 1px solid #ccc;
border-radius: 20px;
background: linear-gradient(#EFEFEF, #EFEFEF);
position: relative;
text-indent: -9999px;
}
Expand All @@ -23,75 +50,91 @@
width: 0;
height: 0;
margin: 0 auto;
border-top: 20px solid transparent;
border-bottom: 20px solid transparent;
border-left: 20px solid red;
border-top: 12px solid transparent;
border-bottom: 12px solid transparent;
border-left: 15px solid #444444;
}

.audioplay-pauseing {
width:100%;
height:100%;
position: relative;

width: 100%;
height: 24px;
position: relative;
}

.audioplay-pauseing:before {
content:"";
width: 10px;
height: 20px;
background: red;
content: "";
width: 25%;
height: 100%;
background: #444444;
border-radius: 5px;
position: absolute;
left: 0;
top:0;
left: 20%;
top: 0;
}

.audioplay-pauseing:after {
content: "";
width: 10px;
height: 20px;
background: red;
width: 25%;
height: 100%;
background: #444444;
border-radius: 5px;
position: absolute;
right: 0;
top:0;
right: 20%;
top: 0;
}
.audioplay-progress{
width:80%;
border:1px solid #ccc;
height:20px;
position: relative;

.audioplay-progress {
width: 60%;
border: 1px solid #ccc;
height: 20px;
position: relative;
margin: 10px 2% 0;
}
.audioplay-progress-status{
height:100%;
background: #000;

.audioplay-progress-status {
height: 100%;
background: #000;
position: absolute;
top:0;
left:0;
top: 0;
left: 0;
z-index: 10;
}
.audioplay-progress-buffered{
height:100%;

.audioplay-progress-buffered {
height: 100%;
background: #ccc;
position: absolute;
top:0;
left:0;
top: 0;
left: 0;
z-index: 1;

}
.audioplay-list li{
cursor: pointer;

.audioplay-time {

margin-top: 10px;
}
.audioplay-list li.on{
color:blue;
.audioplay-time span{
font-size: 14px;
}
.audioplay-volume{
width:50%;
border:1px solid #ccc;
height:20px;
.audioplay-volume {
width: 30%;
border: 1px solid #ccc;
height: 15px;
border-radius: 4px;
margin-left: 70%;
}
.audioplay-volume-status{
height:100%;

.audioplay-volume-status {
height: 100%;
background: #000;
}
}

.audioplay-list li {
cursor: pointer;
padding:8px;
border-bottom: 1px dotted #444;
}

.audioplay-list li.on {
color: blue;
}
Loading

0 comments on commit 978780e

Please sign in to comment.