This commit is contained in:
DIYgod
2015-11-23 00:05:52 +08:00
parent 1a4c0761b1
commit 1d62a152cd
18 changed files with 359 additions and 303 deletions
+234
View File
@@ -0,0 +1,234 @@
function APlayer(option) {
this.option = option;
}
APlayer.prototype.init = function () {
this.element = this.option.element;
this.music = this.option.music;
// fill in HTML
this.element.innerHTML = ''
+ '<div class="aplayer-pic">'
+ '<img src="' + this.music.pic + '">'
+ '<div class="aplayer-button aplayer-pause aplayer-hide">'
+ '<i class="demo-icon aplayer-icon-pause"></i>'
+ '</div>'
+ '<div class="aplayer-button aplayer-play">'
+ '<i class="demo-icon aplayer-icon-play"></i>'
+ '</div>'
+ '</div>'
+ '<div class="aplayer-info">'
+ '<div class="aplayer-music">'
+ '<a href="javascript:void((function(s,d,e,r,l,p,t,z,c){var%20f=\'http://v.t.sina.com.cn/share/share.php?appkey=2992571369\',u=z||d.location,p=[\'&url=\',e(u),\'&title=\',e(t||d.title),\'&source=\',e(r),\'&sourceUrl=\',e(l),\'&content=\',c||\'gb2312\',\'&pic=\',e(p||\'\')].join(\'\');function%20a(){if(!window.open([f,p].join(\'\'),\'mb\',[\'toolbar=0,status=0,resizable=1,width=440,height=430,left=\',(s.width-440)/2,\',top=\',(s.height-430)/2].join(\'\')))u.href=[f,p].join(\'\');};if(/Firefox/.test(navigator.userAgent))setTimeout(a,0);else%20a();})(screen,document,encodeURIComponent,\'\',\'\',\'' + this.music.pic + '\',\'' + '#APlayer音乐分享# ' + this.music.title + ' - ' + this.music.author + ' \',\'\',\'\'));" title="分享至微博"><i class="demo-icon aplayer-icon-weibo"></i></a>'
+ '<span class="aplayer-title">' + this.music.title + '</span>'
+ '<span class="aplayer-author"> - (>﹏<)加载中,好累的说...</span>'
+ '</div>'
+ '<div class="aplayer-controller">'
+ '<div class="aplayer-bar-wrap">'
+ '<div class="aplayer-bar">'
+ '<div class="aplayer-loaded" style="width: 0"></div>'
+ '<div class="aplayer-played" style="width: 0">'
+ '<span class="aplayer-thumb"></span>'
+ '</div>'
+ '</div>'
+ '</div>'
+ '<span class="aplayer-time">'
+ ' - <span class="aplayer-ptime">00:00</span> / <span class="aplayer-dtime">00:00</span>'
+ '<div class="aplayer-volume-wrap">'
+ '<i class="demo-icon aplayer-icon-volume-down"></i>'
+ '<div class="aplayer-volume-bar-wrap">'
+ '<div class="aplayer-volume-bar">'
+ '<div class="aplayer-volume" style="height: 80%"></div>'
+ '</div>'
+ '</div>'
+ '</div>'
+ '</span>'
+ '</div>'
+ '</div>';
// switch to narrow style
if (this.option.narrow) {
this.element.classList.add('aplayer-narrow');
}
// create audio element
this.audio = document.createElement("audio");
this.audio.src = this.music.url;
this.audio.loop = true;
this.audio.preload = 'metadata';
// show audio time
var _self = this;
this.audio.addEventListener('durationchange', function() {
_self.element.getElementsByClassName('aplayer-dtime')[0].innerHTML = _self.secondToTime(_self.audio.duration);
});
// can play: remove loading style, show loading progress bar
this.audio.addEventListener('canplay', function() {
_self.element.getElementsByClassName('aplayer-author')[0].innerHTML = ' - ' + _self.music.author;
_self.loadedTime = setInterval(function () {
var percentage = _self.audio.buffered.end(_self.audio.buffered.length - 1) / _self.audio.duration;
_self.updateBar.call(_self, 'loaded', percentage, 'width');
if (percentage === 1) {
clearInterval(_self.loadedTime);
}
}, 500);
});
this.audio.addEventListener('error', function () {
_self.element.getElementsByClassName('aplayer-author')[0].innerHTML = ' - ' + '加载失败 ╥﹏╥';
});
// play and pause button
this.playButton = this.element.getElementsByClassName('aplayer-play')[0];
this.pauseButton = this.element.getElementsByClassName('aplayer-pause')[0];
this.playButton.addEventListener('click', function () {
_self.play.call(_self);
});
this.pauseButton.addEventListener('click', function () {
_self.pause.call(_self);
});
// control play progress
this.playedBar = this.element.getElementsByClassName('aplayer-played')[0];
this.loadedBar = this.element.getElementsByClassName('aplayer-loaded')[0];
this.thumb = this.element.getElementsByClassName('aplayer-thumb')[0];
this.bar = this.element.getElementsByClassName('aplayer-bar')[0];
var barWidth;
this.bar.addEventListener('click', function (event) {
var e = event || window.event;
barWidth = _self.bar.clientWidth;
var percentage = (e.clientX - getElementViewLeft(_self.bar)) / barWidth;
_self.updateBar.call(_self, 'played', percentage, 'width');
_self.element.getElementsByClassName('aplayer-ptime')[0].innerHTML = _self.secondToTime(percentage * _self.audio.duration);
_self.audio.currentTime = parseFloat(_self.playedBar.style.width) / 100 * _self.audio.duration;
});
this.thumb.addEventListener('mousedown', function () {
barWidth = _self.bar.clientWidth;
clearInterval(_self.playedTime);
document.addEventListener('mousemove', thumbMove);
document.addEventListener('mouseup', thumbUp);
});
function thumbMove (event) {
var e = event || window.event;
var percentage = (e.clientX - getElementViewLeft(_self.bar)) / barWidth;
percentage = percentage > 0 ? percentage : 0;
percentage = percentage < 1 ? percentage : 1;
_self.updateBar.call(_self, 'played', percentage, 'width');
_self.element.getElementsByClassName('aplayer-ptime')[0].innerHTML = _self.secondToTime(percentage * _self.audio.duration);
}
function thumbUp () {
document.removeEventListener('mouseup', thumbUp);
document.removeEventListener('mousemove', thumbMove);
_self.audio.currentTime = parseFloat(_self.playedBar.style.width) / 100 * _self.audio.duration;
_self.playedTime = setInterval(function () {
_self.updateBar.call(_self, 'played', _self.audio.currentTime / _self.audio.duration, 'width');
_self.element.getElementsByClassName('aplayer-ptime')[0].innerHTML = _self.secondToTime(_self.audio.currentTime);
}, 100);
}
// control volume
this.audio.volume = 0.8;
this.volumeBar = this.element.getElementsByClassName('aplayer-volume')[0];
var volumeBarWrap = this.element.getElementsByClassName('aplayer-volume-bar')[0];
var volumeicon = _self.element.getElementsByClassName('aplayer-time')[0].getElementsByTagName('i')[0];
var barHeight = 35;
this.element.getElementsByClassName('aplayer-volume-bar-wrap')[0].addEventListener('click', function (event) {
var e = event || window.event;
var percentage = (barHeight - e.clientY + getElementViewTop(volumeBarWrap)) / barHeight;
percentage = percentage > 0 ? percentage : 0;
percentage = percentage < 1 ? percentage : 1;
_self.updateBar.call(_self, 'volume', percentage, 'height');
_self.audio.volume = percentage;
if (_self.audio.muted) {
_self.audio.muted = false;
}
if (percentage === 1) {
volumeicon.className = 'demo-icon aplayer-icon-volume-up';
}
else {
volumeicon.className = 'demo-icon aplayer-icon-volume-down';
}
});
volumeicon.addEventListener('click', function () {
if (_self.audio.muted) {
_self.audio.muted = false;
volumeicon.className = _self.audio.volume === 1 ? 'demo-icon aplayer-icon-volume-up' : 'demo-icon aplayer-icon-volume-down';
_self.updateBar.call(_self, 'volume', _self.audio.volume, 'height');
}
else {
_self.audio.muted = true;
volumeicon.className = 'demo-icon aplayer-icon-volume-off';
_self.updateBar.call(_self, 'volume', 0, 'height');
}
});
// get element's view position
function getElementViewLeft (element) {
var actualLeft = element.offsetLeft;
var current = element.offsetParent;
var elementScrollLeft;
while (current !== null){
actualLeft += current.offsetLeft;
current = current.offsetParent;
}
elementScrollLeft = document.body.scrollLeft + document.documentElement.scrollLeft;
return actualLeft - elementScrollLeft;
}
function getElementViewTop (element) {
var actualTop = element.offsetTop;
var current = element.offsetParent;
var elementScrollTop;
while (current !== null){
actualTop += current. offsetTop;
current = current.offsetParent;
}
elementScrollTop = document.body.scrollTop + document.documentElement.scrollTop;
return actualTop - elementScrollTop;
}
// autoplay
if (this.option.autoplay) {
this.play();
}
};
// play
APlayer.prototype.play = function () {
this.playButton.classList.add('aplayer-hide');
this.pauseButton.classList.remove('aplayer-hide');
this.audio.play();
var _self = this;
this.playedTime = setInterval(function () {
_self.updateBar.call(_self, 'played', _self.audio.currentTime / _self.audio.duration, 'width');
_self.element.getElementsByClassName('aplayer-ptime')[0].innerHTML = _self.secondToTime(_self.audio.currentTime);
}, 100);
};
// pause
APlayer.prototype.pause = function () {
this.pauseButton.classList.add('aplayer-hide');
this.playButton.classList.remove('aplayer-hide');
this.audio.pause();
clearInterval(this.playedTime);
};
// update progress bar (loading progress bar, play progress bar)
APlayer.prototype.updateBar = function (type, percentage, direction) {
percentage = percentage > 0 ? percentage : 0;
percentage = percentage < 1 ? percentage : 1;
this[type + 'Bar'].style[direction] = percentage * 100 + '%';
};
// format second to 00:00
APlayer.prototype.secondToTime = function (second) {
var add0 = function (num) {
return num < 10 ? '0' + num : '' + num;
};
var min = parseInt(second / 60);
var sec = parseInt(second - min * 60);
return add0(min) + ':' + add0(sec);
};
+280
View File
@@ -0,0 +1,280 @@
$aplayer-height: 66px;
@font-face {
font-family: 'aplayer-fontello';
src: url('font/aplayer-fontello.eot?72550380');
src: url('font/aplayer-fontello.eot?72550380#iefix') format('embedded-opentype'),
url('font/aplayer-fontello.woff?72550380') format('woff'),
url('font/aplayer-fontello.ttf?72550380') format('truetype'),
url('font/aplayer-fontello.svg?72550380#fontello') format('svg');
font-weight: normal;
font-style: normal;
}
.aplayer {
font-family: Arial, Helvetica, sans-serif;
margin: 5px;
box-shadow: 0 2px 2px 0 rgba(0, 0, 0, .14), 0 3px 1px -2px rgba(0, 0, 0, .2), 0 1px 5px 0 rgba(0, 0, 0, .12);
border-radius: 2px;
height: $aplayer-height;
user-select: none;
[class^="aplayer-icon-"]:before, [class*=" aplayer-icon-"]:before {
font-family: "aplayer-fontello";
font-style: normal;
font-weight: normal;
display: inline-block;
text-decoration: inherit;
width: 1em;
text-align: center;
font-variant: normal;
text-transform: none;
line-height: 1em;
}
.aplayer-icon-weibo:before {
content: '\e805';
}
.aplayer-icon-play:before {
content: '\e806';
}
.aplayer-icon-pause:before {
content: '\e807';
}
.aplayer-icon-to-start:before {
content: '\e808';
}
.aplayer-icon-to-end:before {
content: '\e809';
}
.aplayer-icon-list:before {
content: '\e80a';
}
.aplayer-icon-menu:before {
content: '\e80b';
}
.aplayer-icon-volume-off:before {
content: '\e800';
}
.aplayer-icon-volume-down:before {
content: '\e801';
}
.aplayer-icon-volume-up:before {
content: '\e802';
}
.aplayer-narrow {
width: $aplayer-height;
.aplayer-info {
display: none;
}
}
span {
cursor: default !important;
}
.aplayer-pic {
position: relative;
float: left;
height: 100%;
width: $aplayer-height;
img {
height: 100%;
width: 100%;
border-radius: 2px 0 0 2px;
}
.aplayer-button {
position: absolute;
color: #fff;
border-radius: 50%;
opacity: 0.8;
cursor: pointer;
text-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
box-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
background: rgba(0, 0, 0, 0.2);
&:hover {
opacity: 1;
}
}
.aplayer-hide {
display: none;
}
.aplayer-play {
width: 26px;
height: 26px;
border: 2px solid #fff;
top: 50%;
left: 50%;
margin: -15px 0 0 -15px;
.aplayer-icon-play {
position: absolute;
top: 2px;
left: 4px;
font-size: 20px;
line-height: 23px;
}
}
.aplayer-pause {
width: 16px;
height: 16px;
border: 2px solid #fff;
bottom: 4px;
right: 4px;
.aplayer-icon-pause {
position: absolute;
top: 1px;
left: 2px;
font-size: 12px;
line-height: 14px;
}
}
}
.aplayer-info {
margin-left: $aplayer-height;
padding: 17px 7px 0 10px;
.aplayer-music {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
margin-bottom: 17px;
.aplayer-title {
font-size: 14px;
}
.aplayer-author {
font-size: 12px;
color: #666;
}
a {
margin-right: 7px;
font-size: 14px;
color: #000;
&:hover {
color: #E90F24;
}
}
}
.aplayer-controller {
position: relative;
.aplayer-bar-wrap {
margin-right: 120px;
.aplayer-bar {
position: relative;
height: 2px;
width: 100%;
background: #cdcdcd;
cursor: pointer !important;
.aplayer-loaded {
position: absolute;
left: 0;
top: 0;
bottom: 0;
background: #aaa;
height: 2px;
transition: all 0.5s ease;
}
.aplayer-played {
position: absolute;
left: 0;
top: 0;
bottom: 0;
background: #b7daff;
height: 2px;
.aplayer-thumb {
position: absolute;
top: 0;
right: 0;
margin-top: -4px;
margin-right: -10px;
height: 8px;
width: 8px;
border: 1px solid #b7daff;
border-radius: 50%;
background: #fff;
cursor: pointer !important;
&:hover {
background: #b7daff;
}
}
}
}
}
.aplayer-time {
position: absolute;
right: 0;
bottom: -5px;
height: 17px;
color: #999;
font-size: 11px;
i {
color: #666;
font-size: 15px;
}
.aplayer-volume-wrap {
display: inline-block;
margin-left: 7px;
cursor: pointer !important;
&:hover .aplayer-volume-bar-wrap {
display: block;
}
.aplayer-volume-bar-wrap {
display: none;
position: absolute;
bottom: 17px;
right: -5px;
width: 25px;
height: 40px;
.aplayer-volume-bar {
position: absolute;
bottom: 0;
right: 10px;
width: 5px;
height: 35px;
background: #aaa;
.aplayer-volume {
position: absolute;
bottom: 0;
right: 0;
width: 5px;
background: #b7daff;
}
}
}
}
}
}
}
}
Binary file not shown.
+21
View File
@@ -0,0 +1,21 @@
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg">
<metadata>Copyright (C) 2015 by original authors @ fontello.com</metadata>
<defs>
<font id="fontello" horiz-adv-x="1000" >
<font-face font-family="fontello" font-weight="400" font-stretch="normal" units-per-em="1000" ascent="850" descent="-150" />
<missing-glyph horiz-adv-x="1000" />
<glyph glyph-name="volume-off" unicode="&#xe800;" d="m429 654v-608q0-14-11-25t-25-10-25 10l-186 186h-146q-15 0-25 11t-11 25v214q0 15 11 25t25 11h146l186 186q10 10 25 10t25-10 11-25z" horiz-adv-x="428.6" />
<glyph glyph-name="volume-down" unicode="&#xe801;" d="m429 654v-608q0-14-11-25t-25-10-25 10l-186 186h-146q-15 0-25 11t-11 25v214q0 15 11 25t25 11h146l186 186q10 10 25 10t25-10 11-25z m214-304q0-42-24-79t-63-52q-5-3-14-3-14 0-25 10t-10 26q0 12 6 20t17 14 19 12 16 20 6 32-6 32-16 20-19 12-17 14-6 20q0 15 10 26t25 10q9 0 14-3 39-15 63-52t24-79z" horiz-adv-x="642.9" />
<glyph glyph-name="volume-up" unicode="&#xe802;" d="m429 654v-608q0-14-11-25t-25-10-25 10l-186 186h-146q-15 0-25 11t-11 25v214q0 15 11 25t25 11h146l186 186q10 10 25 10t25-10 11-25z m214-304q0-42-24-79t-63-52q-5-3-14-3-14 0-25 10t-10 26q0 12 6 20t17 14 19 12 16 20 6 32-6 32-16 20-19 12-17 14-6 20q0 15 10 26t25 10q9 0 14-3 39-15 63-52t24-79z m143 0q0-85-48-158t-125-105q-8-3-14-3-15 0-26 11t-10 25q0 22 21 33 32 16 43 25 41 30 64 75t23 97-23 97-64 75q-11 9-43 25-21 11-21 33 0 14 10 25t25 11q7 0 15-3 78-33 125-105t48-158z m143 0q0-128-71-236t-189-158q-7-3-14-3-15 0-26 11t-10 25q0 20 22 33 4 2 12 6t13 6q25 14 45 28 69 51 108 127t38 161-38 161-108 127q-20 14-45 28-4 3-13 6t-12 6q-22 13-22 33 0 14 10 25t26 11q7 0 14-3 118-51 189-158t71-236z" horiz-adv-x="928.6" />
<glyph glyph-name="weibo" unicode="&#xe805;" d="m377 134q11 18 6 38t-25 28q-19 8-41 0t-33-25q-13-19-8-38t24-29 42-1 35 27z m52 67q5 7 2 15t-10 10q-7 3-16 0t-12-10q-9-18 8-26 8-2 16 1t12 10z m97-60q-25-57-88-83t-125-7q-60 19-82 71t3 104q27 52 85 78t117 10q62-16 89-66t1-107z m174 90q-5 53-49 94t-117 61-153 12q-124-13-206-79t-74-148q5-53 50-94t116-61 153-12q125 13 206 79t74 148z m172-3q0-38-20-77t-61-77-94-65-126-47-151-17-154 19-134 51-96 85-36 111q0 64 39 137t110 144q94 94 191 131t137-3q36-36 11-117-2-8 0-11t5-4 8 0 8 2l3 1q78 33 138 33t85-34q25-35 0-99-1-8-3-11t3-7 7-5 9-3q32-10 58-26t44-46 19-65z m-41 349q23-27 30-61t-3-66q-5-12-17-19t-25-2q-12 5-19 17t-2 25q11 35-13 61t-60 20q-13-3-25 4t-14 21q-3 14 5 25t20 14q34 7 67-3t56-37z m101 91q48-54 63-125t-8-134q-5-16-19-23t-29-2-22 19-3 29q16 46 6 96t-45 88q-35 39-83 53t-96 5q-16-3-29 6t-17 24 5 29 25 16q68 14 136-6t116-75z" horiz-adv-x="1000" />
<glyph glyph-name="play" unicode="&#xe806;" d="m486 376q14-10 14-26 0-14-14-24l-428-266q-24-16-41-6t-17 40l0 514q0 30 17 40t41-6z" horiz-adv-x="500" />
<glyph glyph-name="pause" unicode="&#xe807;" d="m440 700q90 0 90-64l0-570q0-66-90-66t-90 66l0 570q0 64 90 64z m-350 0q90 0 90-64l0-570q0-66-90-66t-90 66l0 570q0 64 90 64z" horiz-adv-x="530" />
<glyph glyph-name="to-start" unicode="&#xe808;" d="m174 350q0 14 14 24l364 228q20 14 34 5t14-35l0-442q0-26-14-35t-34 5l-364 228q-14 10-14 22z m-174 234q0 58 76 58 74 0 74-58l0-466q0-58-74-58-76 0-76 58l0 466z" horiz-adv-x="600" />
<glyph glyph-name="to-end" unicode="&#xe809;" d="m412 374q14-10 14-24 0-12-14-22l-362-228q-22-14-36-5t-14 35l0 442q0 26 14 35t36-5z m114 268q74 0 74-58l0-466q0-58-74-58-76 0-76 58l0 466q0 58 76 58z" horiz-adv-x="600" />
<glyph glyph-name="list" unicode="&#xe80a;" d="m100 200q20 0 35-15t15-35-15-35-35-15l-50 0q-20 0-35 15t-15 35 14 35 36 15l50 0z m0 200q20 0 35-15t15-35-15-35-35-15l-50 0q-20 0-35 15t-15 35 14 35 36 15l50 0z m0 200q20 0 35-15t15-35-15-35-35-15l-50 0q-20 0-35 15t-15 35 14 35 36 15l50 0z m200-100q-20 0-35 15t-15 35 15 35 35 15l350 0q22 0 36-15t14-35-15-35-35-15l-350 0z m350-100q22 0 36-15t14-35-15-35-35-15l-350 0q-20 0-35 15t-15 35 15 35 35 15l350 0z m0-200q22 0 36-15t14-35-15-35-35-15l-350 0q-20 0-35 15t-15 35 15 35 35 15l350 0z" horiz-adv-x="700" />
<glyph glyph-name="menu" unicode="&#xe80b;" d="m650 400q22 0 36-15t14-35-15-35-35-15l-600 0q-20 0-35 15t-15 35 14 35 36 15l600 0z m-600 100q-20 0-35 15t-15 35 14 35 36 15l600 0q22 0 36-15t14-35-15-35-35-15l-600 0z m600-300q22 0 36-15t14-35-15-35-35-15l-600 0q-20 0-35 15t-15 35 14 35 36 15l600 0z" horiz-adv-x="700" />
</font>
</defs>
</svg>

After

Width:  |  Height:  |  Size: 4.3 KiB

Binary file not shown.
Binary file not shown.