9 Commits
Author SHA1 Message Date
DIYgod 097fd27caa update console version 2016-10-14 10:36:02 +08:00
DIYgod 5ce630df04 Optimize sorting algorithm for random mode, fix: #77 2016-10-14 10:26:50 +08:00
DIYgod c1c83d3c96 optimize the style of aplayer with lrc 2016-10-11 11:24:03 +08:00
DIYgod 89992c84c9 fix aplayer-bar width 2016-10-11 10:02:15 +08:00
Wenjie Fan 58a53e324c Merge pull request #76 from argvil19/display-date
display hh:mm:ss format when audio is over an hour long
2016-10-10 20:41:15 -05:00
Argenis Villasmil 02058ad17f display hh:mm:ss format when audio is over an hour long 2016-10-10 21:01:30 -04:00
DIYgod d7365d9546 Make music title and lrc selectable; click list to play next song in mobile: close #69 2016-09-30 11:13:35 +08:00
DIYgod 1386beaa27 Fix UMD bug 2016-09-26 01:50:13 +08:00
DIYgod 62d146a9dc Scrollbar style, close #72 2016-09-20 12:12:21 +08:00
8 changed files with 169 additions and 67 deletions
+7 -1
View File
@@ -54,7 +54,7 @@ var option = {
mutex: true, // Optional, pause other players when this player playing mutex: true, // Optional, pause other players when this player playing
theme: '#e6d0b2', // Optional, theme color, default: #b7daff theme: '#e6d0b2', // Optional, theme color, default: #b7daff
mode: 'random', // Optional, play mode, can be `random` `single` `circulation`(loop) `order`(no loop), default: `circulation` mode: 'random', // Optional, play mode, can be `random` `single` `circulation`(loop) `order`(no loop), default: `circulation`
preload: 'metadata', // Optional, the way to load music, can be 'none' 'metadata' 'auto', default: 'metadata' in Desktop, 'none' in mobile preload: 'metadata', // Optional, the way to load music, can be 'none' 'metadata' 'auto', default: 'auto'
listmaxheight: '513px', // Optional, max height of play list listmaxheight: '513px', // Optional, max height of play list
music: { // Required, music info, see: ###With playlist music: { // Required, music info, see: ###With playlist
title: 'Preparation', // Required, music title title: 'Preparation', // Required, music title
@@ -74,6 +74,12 @@ var option = {
+ `ap.toggle()` // Toggle between play and pause + `ap.toggle()` // Toggle between play and pause
+ `ap.volume(percentage)` // Set volume + `ap.volume(percentage)` // Set volume
+ `ap.on(event, handler)` // Event binding + `ap.on(event, handler)` // Event binding
+ `ap.setMusic(index)` // Switch music
+ `ap.lrc` // Lrc time and text
+ `ap.playIndex` // Current playing index
+ `ap.audio` // Return native video, most [native api](http://www.w3schools.com/tags/ref_av_dom.asp) are supported
+ `dp.audio.currentTime` // Returns the current playback position
+ Most [native api](http://www.w3schools.com/tags/ref_av_dom.asp)
#### Event binding #### Event binding
+1 -1
View File
@@ -107,7 +107,7 @@ var ap5 = new APlayer({
mutex: true, mutex: true,
theme: '#ad7a86', theme: '#ad7a86',
mode: 'random', mode: 'random',
listmaxheight: '60px', listmaxheight: '80px',
music: [ music: [
{ {
title: 'あっちゅ~ま青春!', title: 'あっちゅ~ま青春!',
+2 -2
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
File diff suppressed because one or more lines are too long
+2 -2
View File
@@ -1,11 +1,11 @@
{ {
"name": "aplayer", "name": "aplayer",
"version": "1.5.2", "version": "1.5.6",
"description": "Wow, such a beautiful html5 music player", "description": "Wow, such a beautiful html5 music player",
"main": "dist/APlayer.min.js", "main": "dist/APlayer.min.js",
"scripts": { "scripts": {
"test": "webpack", "test": "webpack",
"start": "webpack-dev-server --hot --progress --inline" "start": "webpack-dev-server --hot --progress --inline --host 0.0.0.0"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
+92 -39
View File
@@ -1,4 +1,4 @@
console.log("\n %c APlayer 1.5.2 %c http://aplayer.js.org \n\n","color: #fadfa3; background: #030307; padding:5px 0;","background: #fadfa3; padding:5px 0;"); console.log("\n %c APlayer 1.5.6 %c http://aplayer.js.org \n\n","color: #fadfa3; background: #030307; padding:5px 0;","background: #fadfa3; padding:5px 0;");
require('./APlayer.scss'); require('./APlayer.scss');
@@ -63,10 +63,10 @@ class APlayer {
this.mode = option.mode; this.mode = option.mode;
/** /**
* Parse second to 00:00 format * Parse second to 00:00 format. 00:00:00 if audio is over an hour long.
* *
* @param {Number} second * @param {Number} second
* @return {String} 00:00 format * @return {String} 00:00 format. 00:00:00 if over an hour long.
*/ */
this.secondToTime = (second) => { this.secondToTime = (second) => {
const add0 = (num) => { const add0 = (num) => {
@@ -74,7 +74,9 @@ class APlayer {
}; };
const min = parseInt(second / 60); const min = parseInt(second / 60);
const sec = parseInt(second - min * 60); const sec = parseInt(second - min * 60);
return add0(min) + ':' + add0(sec); const hours = parseInt(min / 60);
const minAdjust = parseInt((second / 60) - (60 * parseInt((second / 60) / 60)));
return second >= 3600 ? add0(hours) + ':' + add0(minAdjust) + ':' + add0(sec) : add0(min) + ':' + add0(sec);
}; };
// save lrc // save lrc
@@ -110,8 +112,8 @@ class APlayer {
for (let i = 0; i < this.lrc.length; i++) { for (let i = 0; i < this.lrc.length; i++) {
if (currentTime >= this.lrc[i][0] && (!this.lrc[i + 1] || currentTime < this.lrc[i + 1][0])) { if (currentTime >= this.lrc[i][0] && (!this.lrc[i + 1] || currentTime < this.lrc[i + 1][0])) {
this.lrcIndex = i; this.lrcIndex = i;
this.lrcContents.style.transform = `translateY(${-this.lrcIndex * 20}px)`; this.lrcContents.style.transform = `translateY(${-this.lrcIndex * 16}px)`;
this.lrcContents.style.webkitTransform = `translateY(${-this.lrcIndex * 20}px)`; this.lrcContents.style.webkitTransform = `translateY(${-this.lrcIndex * 16}px)`;
this.lrcContents.getElementsByClassName('aplayer-lrc-current')[0].classList.remove('aplayer-lrc-current'); this.lrcContents.getElementsByClassName('aplayer-lrc-current')[0].classList.remove('aplayer-lrc-current');
this.lrcContents.getElementsByTagName('p')[i].classList.add('aplayer-lrc-current'); this.lrcContents.getElementsByTagName('p')[i].classList.add('aplayer-lrc-current');
} }
@@ -138,6 +140,14 @@ class APlayer {
if (this.option.showlrc) { if (this.option.showlrc) {
this.element.classList.add('aplayer-withlrc'); this.element.classList.add('aplayer-withlrc');
} }
if (this.option.music.length > 1) {
this.element.classList.add('aplayer-list');
}
if (!this.multiple && this.mode !== 'circulation' && this.mode !== 'order') {
this.mode = 'circulation';
}
this.getRandomOrder();
// fill in HTML // fill in HTML
let eleHTML = ` let eleHTML = `
@@ -170,7 +180,7 @@ class APlayer {
- <span class="aplayer-ptime">00:00</span> / <span class="aplayer-dtime">00:00</span> - <span class="aplayer-ptime">00:00</span> / <span class="aplayer-dtime">00:00</span>
</span> </span>
<div class="aplayer-volume-wrap"> <div class="aplayer-volume-wrap">
<button class="aplayer-icon aplayer-icon-volume-down">` <button class="aplayer-icon aplayer-icon-volume-down" ${this.isMobile ? 'style="display: none;"' : ''}>`
+ this.getSVG('volume-down') + this.getSVG('volume-down')
+ ` </button> + ` </button>
<div class="aplayer-volume-bar-wrap"> <div class="aplayer-volume-bar-wrap">
@@ -220,7 +230,6 @@ class APlayer {
// fix the width of aplayer bar // fix the width of aplayer bar
let bar = {}; let bar = {};
bar.barWrap = this.element.getElementsByClassName('aplayer-bar-wrap')[0]; bar.barWrap = this.element.getElementsByClassName('aplayer-bar-wrap')[0];
bar.barWrap.style.marginRight = this.element.getElementsByClassName('aplayer-time')[0].offsetWidth + 5 + 'px';
// switch to narrow style // switch to narrow style
if (this.option.narrow) { if (this.option.narrow) {
@@ -246,12 +255,7 @@ class APlayer {
const musicIndex = parseInt(listItem[i].getElementsByClassName('aplayer-list-index')[0].innerHTML) - 1; const musicIndex = parseInt(listItem[i].getElementsByClassName('aplayer-list-index')[0].innerHTML) - 1;
if (musicIndex !== this.playIndex) { if (musicIndex !== this.playIndex) {
this.setMusic(musicIndex); this.setMusic(musicIndex);
if (this.isMobile) { this.play();
this.pause();
}
else {
this.play();
}
} }
else { else {
this.toggle(); this.toggle();
@@ -376,17 +380,27 @@ class APlayer {
// mode control // mode control
const modeEle = this.element.getElementsByClassName('aplayer-icon-mode')[0]; const modeEle = this.element.getElementsByClassName('aplayer-icon-mode')[0];
modeEle.addEventListener('click', () => { modeEle.addEventListener('click', () => {
if (this.mode === 'random') { if (this.multiple) {
this.mode = 'single'; if (this.mode === 'random') {
this.mode = 'single';
}
else if (this.mode === 'single') {
this.mode = 'order';
}
else if (this.mode === 'order') {
this.mode = 'circulation';
}
else if (this.mode === 'circulation') {
this.mode = 'random';
}
} }
else if (this.mode === 'single') { else {
this.mode = 'order'; if (this.mode === 'circulation') {
} this.mode = 'order';
else if (this.mode === 'order') { }
this.mode = 'circulation'; else {
} this.mode = 'circulation';
else if (this.mode === 'circulation') { }
this.mode = 'random';
} }
modeEle.innerHTML = this.getSVG(this.mode); modeEle.innerHTML = this.getSVG(this.mode);
this.audio.loop = !(this.multiple || this.mode === 'order'); this.audio.loop = !(this.multiple || this.mode === 'order');
@@ -407,8 +421,7 @@ class APlayer {
} }
if (this.mode === 'random') { if (this.mode === 'random') {
let random = parseInt(Math.random() * this.option.music.length); this.setMusic(this.randomOrder[0]);
this.setMusic(random);
} }
else { else {
this.setMusic(0); this.setMusic(0);
@@ -455,12 +468,7 @@ class APlayer {
if ((this.multiple && !this.audios[indexMusic]) || this.playIndex === -1) { if ((this.multiple && !this.audios[indexMusic]) || this.playIndex === -1) {
this.audio = document.createElement("audio"); this.audio = document.createElement("audio");
this.audio.src = this.music.url; this.audio.src = this.music.url;
if (this.option.preload) { this.audio.preload = this.option.preload ? this.option.preload : 'auto';
this.audio.preload = this.option.preload;
}
else {
this.audio.preload = this.isMobile ? 'none' : 'metadata';
}
// show audio time: the metadata has loaded or changed // show audio time: the metadata has loaded or changed
this.audio.addEventListener('durationchange', () => { this.audio.addEventListener('durationchange', () => {
@@ -497,12 +505,7 @@ class APlayer {
} }
if (this.audio.currentTime !== 0) { if (this.audio.currentTime !== 0) {
if (this.mode === 'random') { if (this.mode === 'random') {
let random = parseInt(Math.random() * this.option.music.length); this.setMusic(this.nextRandomNum());
if (random === this.playIndex) {
random++;
random = random % this.option.music.length;
}
this.setMusic(random);
} }
else if (this.mode === 'single') { else if (this.mode === 'single') {
this.setMusic(this.playIndex); this.setMusic(this.playIndex);
@@ -783,6 +786,56 @@ class APlayer {
this.pause(); this.pause();
} }
} }
/**
* get random order, using Fisher–Yates shuffle
*/
getRandomOrder() {
function random(min, max) {
if (max == null) {
max = min;
min = 0;
}
return min + Math.floor(Math.random() * (max - min + 1));
}
function shuffle(arr) {
var length = arr.length,
shuffled = new Array(length);
for (var index = 0, rand; index < length; index++) {
rand = random(0, index);
if (rand !== index) shuffled[index] = shuffled[rand];
shuffled[rand] = arr[index];
}
return shuffled;
}
if (this.multiple) {
if (!this.normalOrder) {
this.normalOrder = [];
for (let i = 0; i < this.option.music.length; i++) {
this.normalOrder[i] = i;
}
}
this.randomOrder = shuffle(this.normalOrder);
}
}
/**
* get next random number
*/
nextRandomNum() {
if (this.multiple) {
let index = this.randomOrder.indexOf(this.playIndex);
if (index === this.randomOrder.length - 1) {
return this.randomOrder[0];
}
else {
return this.randomOrder[index + 1];
}
}
else {
return 0;
}
}
} }
export {APlayer}; module.exports = APlayer;
+63 -21
View File
@@ -1,6 +1,6 @@
$aplayer-height: 66px; $aplayer-height: 66px;
$lrc-height: 40px; $lrc-height: 30px;
$aplayer-height-lrc: $aplayer-height + $lrc-height; $aplayer-height-lrc: $aplayer-height + $lrc-height - 6;
.aplayer-narrow { .aplayer-narrow {
width: $aplayer-height; width: $aplayer-height;
@@ -13,16 +13,32 @@ $aplayer-height-lrc: $aplayer-height + $lrc-height;
&.aplayer-narrow { &.aplayer-narrow {
width: $aplayer-height-lrc; width: $aplayer-height-lrc;
} }
&.aplayer .aplayer-pic { &.aplayer {
height: $aplayer-height-lrc; .aplayer-pic {
width: $aplayer-height-lrc; height: $aplayer-height-lrc;
width: $aplayer-height-lrc;
}
.aplayer-info {
margin-left: $aplayer-height-lrc;
height: $aplayer-height-lrc;
}
.aplayer-lrc {
display: block;
}
.aplayer-info {
padding: 10px 7px 0 7px;
}
} }
&.aplayer .aplayer-info { }
margin-left: $aplayer-height-lrc;
height: $aplayer-height-lrc; .aplayer-list {
} &.aplayer {
&.aplayer .aplayer-lrc { .aplayer-info {
display: block; border-bottom: 1px solid #e9e9e9;
}
} }
} }
@@ -133,6 +149,9 @@ $aplayer-height-lrc: $aplayer-height + $lrc-height;
white-space: nowrap; white-space: nowrap;
text-overflow: ellipsis; text-overflow: ellipsis;
margin-bottom: 13px; margin-bottom: 13px;
user-select: text;
cursor: default;
padding-bottom: 2px;
.aplayer-title { .aplayer-title {
font-size: 14px; font-size: 14px;
@@ -146,11 +165,13 @@ $aplayer-height-lrc: $aplayer-height + $lrc-height;
.aplayer-controller { .aplayer-controller {
position: relative; position: relative;
display: flex;
.aplayer-bar-wrap { .aplayer-bar-wrap {
margin: 0 140px 0 5px; margin: 0 0 0 5px;
padding: 4px 0; padding: 4px 0;
cursor: pointer !important; cursor: pointer !important;
flex: 1;
.aplayer-bar { .aplayer-bar {
position: relative; position: relative;
@@ -192,12 +213,13 @@ $aplayer-height-lrc: $aplayer-height + $lrc-height;
} }
.aplayer-time { .aplayer-time {
position: absolute; position: relative;
right: 0; right: 0;
bottom: -3px; bottom: 3px;
height: 17px; height: 17px;
color: #999; color: #999;
font-size: 11px; font-size: 11px;
padding-left: 7px;
.aplayer-time-inner { .aplayer-time-inner {
vertical-align: middle; vertical-align: middle;
@@ -280,7 +302,7 @@ $aplayer-height-lrc: $aplayer-height + $lrc-height;
background: #fff; background: #fff;
text-align: center; text-align: center;
overflow: hidden; overflow: hidden;
margin: -10px 0 10px; margin: -10px 0 7px;
&:before { &:before {
position: absolute; position: absolute;
@@ -306,17 +328,17 @@ $aplayer-height-lrc: $aplayer-height + $lrc-height;
width: 100%; width: 100%;
height: 33%; height: 33%;
content: ' '; content: ' ';
background: -moz-linear-gradient(bottom, rgba(255,255,255,1) 0%, rgba(255,255,255,0) 100%); background: -moz-linear-gradient(top, rgba(255,255,255,0) 0%, rgba(255,255,255,0.8) 100%);
background: -webkit-linear-gradient(bottom, rgba(255,255,255,1) 0%,rgba(255,255,255,0) 100%); background: -webkit-linear-gradient(top, rgba(255,255,255,0) 0%,rgba(255,255,255,0.8) 100%);
background: linear-gradient(to top, rgba(255,255,255,1) 0%,rgba(255,255,255,0) 100%); background: linear-gradient(to bottom, rgba(255,255,255,0) 0%,rgba(255,255,255,0.8) 100%);
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#00ffffff', endColorstr='#ffffff',GradientType=0 ); filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#00ffffff', endColorstr='#ccffffff',GradientType=0 );
} }
p { p {
font-size: 12px; font-size: 12px;
color: #666; color: #666;
line-height: 20px !important; line-height: 16px !important;
height: 20px !important; height: 16px !important;
padding: 0 !important; padding: 0 !important;
margin: 0 !important; margin: 0 !important;
transition: all 0.5s ease-out; transition: all 0.5s ease-out;
@@ -331,6 +353,8 @@ $aplayer-height-lrc: $aplayer-height + $lrc-height;
.aplayer-lrc-contents { .aplayer-lrc-contents {
width: 100%; width: 100%;
transition: all 0.5s ease-out; transition: all 0.5s ease-out;
user-select: text;
cursor: default;
} }
} }
@@ -343,6 +367,20 @@ $aplayer-height-lrc: $aplayer-height + $lrc-height;
height: 0 !important; height: 0 !important;
} }
&::-webkit-scrollbar{
width: 5px;
}
&::-webkit-scrollbar-track{
background-color: #f9f9f9;
}
&::-webkit-scrollbar-thumb{
border-radius: 3px;
background-color: #eee;
}
&::-webkit-scrollbar-thumb:hover{
background-color: #ccc;
}
ol { ol {
list-style-type: none; list-style-type: none;
margin: 0; margin: 0;
@@ -359,6 +397,10 @@ $aplayer-height-lrc: $aplayer-height + $lrc-height;
transition: all 0.2s ease; transition: all 0.2s ease;
overflow: hidden; overflow: hidden;
&:first-child {
border-top: none;
}
&:hover { &:hover {
background: #efefef; background: #efefef;
} }
+1
View File
@@ -14,6 +14,7 @@ module.exports = {
output: { output: {
path: BUILD_PATH, path: BUILD_PATH,
filename: 'APlayer.min.js', filename: 'APlayer.min.js',
library: 'APlayer',
libraryTarget: 'umd', libraryTarget: 'umd',
umdNamedDefine: true umdNamedDefine: true
}, },