24 Commits
Author SHA1 Message Date
DIYgod cbc0f99eee new api: destroy, addMusic 2017-03-26 23:31:22 +08:00
DIYgod c3d98f3aca update devDependencies 2017-03-10 18:39:51 +08:00
DIYgod bf566955a8 add CDN unpkg 2017-03-08 19:00:19 +08:00
DIYgod 960b8b34fd add ISSUE_TEMPLATE 2017-03-08 18:50:13 +08:00
DIYgod d381582fe4 prevent buttons from submitting forms 2017-02-09 11:22:58 +08:00
DIYgod 3d64d42305 upgrade node version in travis-ci 2017-02-06 12:27:15 +08:00
DIYgod 7b4d2320a8 use yarn, upgrade to webpack2 2017-02-06 12:12:25 +08:00
DIYgod 6f2174cab4 update README: npm script 2017-01-19 18:21:18 +08:00
DIYgod ef1ae2b761 optimize webpack workflow 2017-01-19 18:18:04 +08:00
Wenjie Fan 6f496ac41c Merge pull request #112 from zanseven007/master
update copyright
2016-12-30 18:48:59 +08:00
Zanyuyu 287950513d update copyright
update copyright for 2017
2016-12-30 18:42:22 +08:00
DIYgod 584f11ce04 update dev-devdependencies 2016-12-22 00:20:04 +08:00
DIYgod 53096c4e0b unify ap.play() with ap.audio.play(), close #98 2016-12-22 00:14:12 +08:00
DIYgod c4c3ead825 fix NaN before canplay, close #94 2016-12-21 18:48:02 +08:00
DIYgod a0467709eb Fix lrc background, close #100 2016-12-01 09:56:20 +08:00
DIYgod 230f43f41c fix docs link 2016-11-28 16:12:24 +08:00
DIYgod a2c9072c1f update devDependencies 2016-11-28 15:35:50 +08:00
DIYgod 9c13dfa7f6 update devDependencies 2016-11-28 15:33:04 +08:00
DIYgod 4c20d7f8be update devDependencies; add docs in README 2016-11-28 15:30:05 +08:00
DIYgod b74b5b0802 add vue-aplayer 2016-11-23 16:47:08 +08:00
DIYgod eec8c3a0ed add BootCDN 2016-11-05 21:18:57 +08:00
DIYgod 3877378602 fix switching music bug, close #83 #85 2016-10-26 21:17:31 +08:00
Wenjie Fan 370d039099 Merge pull request #84 from sabrinaluo/patch-1
Update README.md
2016-10-26 02:15:15 -05:00
HiiTea 634ad2def4 Update README.md 2016-10-26 15:13:06 +08:00
12 changed files with 4100 additions and 365 deletions
+7
View File
@@ -0,0 +1,7 @@
中文用户请注意:请尽量用**英文**描述你的 issue,这样能够让尽可能多的人帮到你。
If you want to report a bug, please provide the following information:
- The steps to reproduce.
- A minimal demo of the problem via https://jsfiddle.net or http://codepen.io/pen if possible.
- Which versions of APlayer, and which browser / OS are affected by this issue?
+1 -1
View File
@@ -1,3 +1,3 @@
language: node_js language: node_js
node_js: node_js:
- "4.0" - "6.1"
+1 -1
View File
@@ -1,6 +1,6 @@
The Star And Thank Author License (SATA) The Star And Thank Author License (SATA)
Copyright (c) 2016 DIYgod(i@html.love) Copyright (c) 2016-2017 DIYgod(i@html.love)
Project Url: https://github.com/DIYgod/APlayer Project Url: https://github.com/DIYgod/APlayer
+9 -163
View File
@@ -14,6 +14,8 @@
[Demo](http://aplayer.js.org) [Demo](http://aplayer.js.org)
[Docs](http://aplayer.js.org/docs)
Using APlayer on your project? [Let me know!](https://github.com/DIYgod/APlayer/issues/79) Using APlayer on your project? [Let me know!](https://github.com/DIYgod/APlayer/issues/79)
Screenshot Screenshot
@@ -27,173 +29,15 @@ $ npm install aplayer --save
## CDN ## CDN
[unpkg](https://unpkg.com/aplayer)
[cdnjs](https://cdnjs.com/libraries/aplayer) [cdnjs](https://cdnjs.com/libraries/aplayer)
[BootCDN](http://www.bootcdn.cn/aplayer)
## Usage ## Usage
### HTML See [docs](http://aplayer.js.org/docs)
```HTML
<div id="player1" class="aplayer"></div>
<!-- ... -->
<script src="APlayer.min.js"></script>
```
### JS
```JS
var ap = new APlayer(option);
```
#### Options
```JS
var option = {
element: document.getElementById('player1'), // Optional, player element
narrow: false, // Optional, narrow style
autoplay: true, // Optional, autoplay song(s), not supported by mobile browsers
showlrc: 0, // Optional, show lrc, can be 0, 1, 2, see: ###With lrc
mutex: true, // Optional, pause other players when this player playing
theme: '#e6d0b2', // Optional, theme color, default: #b7daff
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: 'auto'
listmaxheight: '513px', // Optional, max height of play list
music: { // Required, music info, see: ###With playlist
title: 'Preparation', // Required, music title
author: 'Hans Zimmer/Richard Harvey', // Required, music author
url: 'http://7xifn9.com1.z0.glb.clouddn.com/Preparation.mp3', // Required, music url
pic: 'http://7xifn9.com1.z0.glb.clouddn.com/Preparation.jpg', // Optional, music picture
lrc: '[00:00.00]lrc here\n[00:01.00]aplayer' // Optional, lrc, see: ###With lrc
}
}
```
#### API
+ `ap.play()` // Resume play
+ `ap.play(time)` // Set currentTime
+ `ap.pause()` // Pause
+ `ap.toggle()` // Toggle between play and pause
+ `ap.volume(percentage)` // Set volume
+ `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
`ap.on(event, handler)`
`event`:
+ `play`: Triggered when APlayer start play
+ `pause`: Triggered when APlayer paused
+ `canplay`: Triggered when enough data is available that APlayer can be played
+ `playing`: Triggered periodically when APlayer is playing
+ `ended`: Triggered when APlayer ended
+ `error`: Triggered when an error occurs
#### Work with module bundler
```js
var APlayer = require('APlayer');
var ap = new APlayer({
// ...
});
```
### With lrc
Show lrc, you can put LRC in JS or HTML as you like.
#### LRC format:
Support multiple time tag, support three decimal second
```
[mm:ss.xx]lyric
[mm:ss.xxx]lyric
[mm:ss.xx][mm:ss.xx][mm:ss.xx]lyric
...
```
#### LRC in JS:
JS:
```js
{
showlrc: 1,
music: {
lrc: '[00:00.00]lrc here\n[00:01.00]aplayer' // lrc here, separate lines with \n
}
}
```
#### LRC in HTML:
JS:
```js
{
showlrc: 2
}
```
HTML:
```HTML
<div id="player1" class="aplayer">
<pre class="aplayer-lrc-content">
[00:00.00]平凡之路 - 朴树
[00:04.01]作词:韩寒 朴树
[00:08.02]作曲:朴树 编曲:朴树
[00:12.02]徘徊着的 在路上的
[00:17.37]你要走吗
[00:23.20]易碎的 骄傲着
<!-- ... -->
</pre>
</div>
```
#### LRC in lrc file or API:
```js
{
showlrc: 3,
music: {
lrc: 'path/to/music.lrc'
}
}
```
### With playlist
Show multiple music.
#### JS:
Option:
```JS
music: [
{
title: '',
author: '',
url: '',
pic: ''
},
{
title: '',
author: '',
url: '',
pic: ''
},
...
]
```
## Run in development ## Run in development
@@ -219,6 +63,8 @@ $ npm run build
- [Hermit-X(APlayer for WordPress)](https://github.com/liwanglin12/Hermit-X) - [Hermit-X(APlayer for WordPress)](https://github.com/liwanglin12/Hermit-X)
- [vue-aplayer](https://github.com/SevenOutman/vue-aplayer)
## LICENSE ## LICENSE
[The Star And Thank Author License (SATA)](https://github.com/DIYgod/APlayer/blob/master/LICENSE) [The Star And Thank Author License (SATA)](https://github.com/DIYgod/APlayer/blob/master/LICENSE)
+7 -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
+16 -14
View File
@@ -1,15 +1,16 @@
{ {
"name": "aplayer", "name": "aplayer",
"version": "1.5.7", "version": "1.6.0",
"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 --host 0.0.0.0" "build": "webpack",
"dev": "WEBPACK_ENV=dev webpack-dev-server --hot --progress --inline --host 0.0.0.0"
}, },
"repository": { "repository": {
"type": "git", "url": "git+https://github.com/DIYgod/APlayer.git",
"url": "git+https://github.com/DIYgod/APlayer.git" "type": "git"
}, },
"keywords": [ "keywords": [
"player", "player",
@@ -23,18 +24,19 @@
}, },
"homepage": "https://github.com/DIYgod/APlayer#readme", "homepage": "https://github.com/DIYgod/APlayer#readme",
"devDependencies": { "devDependencies": {
"autoprefixer": "^6.4.0", "autoprefixer": "^6.7.2",
"babel-loader": "^6.2.4", "babel-core": "^6.22.1",
"babel-preset-es2015": "^6.13.2", "babel-loader": "^6.2.10",
"css-loader": "^0.25.0", "babel-preset-es2015": "^6.22.0",
"css-loader": "^0.27.1",
"exports-loader": "^0.6.3", "exports-loader": "^0.6.3",
"file-loader": "^0.9.0", "file-loader": "^0.10.0",
"node-sass": "^3.8.0", "node-sass": "^4.5.0",
"postcss-loader": "^1.0.0", "postcss-loader": "^1.2.2",
"sass-loader": "^4.0.0", "sass-loader": "^6.0.3",
"style-loader": "^0.13.1", "style-loader": "^0.13.1",
"url-loader": "^0.5.7", "url-loader": "^0.5.7",
"webpack": "^1.13.1", "webpack": "^2.2.1",
"webpack-dev-server": "^1.14.1" "webpack-dev-server": "^2.3.0"
} }
} }
+5
View File
@@ -0,0 +1,5 @@
module.exports = {
plugins: [
require('autoprefixer')
]
}
+203 -156
View File
@@ -1,4 +1,4 @@
console.log("\n %c APlayer 1.5.7 %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.6.0 %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');
@@ -55,9 +55,6 @@ class APlayer {
} }
} }
// multiple music
this.playIndex = Object.prototype.toString.call(option.music) === '[object Array]' ? 0 : -1;
this.option = option; this.option = option;
this.audios = []; this.audios = [];
this.mode = option.mode; this.mode = option.mode;
@@ -69,6 +66,9 @@ class APlayer {
* @return {String} 00:00 format. 00:00:00 if over an hour long. * @return {String} 00:00 format. 00:00:00 if over an hour long.
*/ */
this.secondToTime = (second) => { this.secondToTime = (second) => {
if (isNaN(second)) {
return '00:00';
}
const add0 = (num) => { const add0 = (num) => {
return num < 10 ? '0' + num : '' + num; return num < 10 ? '0' + num : '' + num;
}; };
@@ -133,15 +133,20 @@ class APlayer {
} }
}; };
this.multiple = this.playIndex > -1; // multiple music
this.music = this.multiple ? this.option.music[this.playIndex] : this.option.music; this.playIndex = 0;
this.multiple = Object.prototype.toString.call(option.music) === '[object Array]';
if (!this.multiple) {
this.option.music = [this.option.music];
}
this.music = this.option.music[this.playIndex];
// add class aplayer-withlrc // add class aplayer-withlrc
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) { if (this.option.music.length > 1) {
this.element.classList.add('aplayer-list'); this.element.classList.add('aplayer-withlist');
} }
if (!this.multiple && this.mode !== 'circulation' && this.mode !== 'order') { if (!this.multiple && this.mode !== 'circulation' && this.mode !== 'order') {
@@ -153,7 +158,7 @@ class APlayer {
let eleHTML = ` let eleHTML = `
<div class="aplayer-pic" ${(this.music.pic ? (`style="background-image: url('${this.music.pic}');"`) : ``)}> <div class="aplayer-pic" ${(this.music.pic ? (`style="background-image: url('${this.music.pic}');"`) : ``)}>
<div class="aplayer-button aplayer-play"> <div class="aplayer-button aplayer-play">
<button class="aplayer-icon aplayer-icon-play">` <button type="button" class="aplayer-icon aplayer-icon-play">`
+ this.getSVG('play') + this.getSVG('play')
+ ` </button> + ` </button>
</div> </div>
@@ -180,7 +185,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" ${this.isMobile ? 'style="display: none;"' : ''}>` <button type="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">
@@ -189,17 +194,15 @@ class APlayer {
</div> </div>
</div> </div>
</div> </div>
<button class="aplayer-icon aplayer-icon-mode">` <button type="button" class="aplayer-icon aplayer-icon-mode">`
+ this.getSVG(this.mode) + this.getSVG(this.mode)
+ ` </button> + ` </button>
${(this.multiple ? `<button class="aplayer-icon aplayer-icon-menu">` <button type="button" class="aplayer-icon aplayer-icon-menu">`
+ this.getSVG('menu') + this.getSVG('menu')
+ ` </button>` : ``)} + ` </button>
</div> </div>
</div> </div>
</div>`; </div>
if (this.multiple) {
eleHTML += `
<div class="aplayer-list" ${this.option.listmaxheight ? `style="max-height: ${this.option.listmaxheight}` : ``}"> <div class="aplayer-list" ${this.option.listmaxheight ? `style="max-height: ${this.option.listmaxheight}` : ``}">
<ol>`; <ol>`;
for (let i = 0; i < this.option.music.length; i++) { for (let i = 0; i < this.option.music.length; i++) {
@@ -214,7 +217,6 @@ class APlayer {
eleHTML += ` eleHTML += `
</ol> </ol>
</div>` </div>`
}
this.element.innerHTML = eleHTML; this.element.innerHTML = eleHTML;
// hide mode button in arrow container // hide mode button in arrow container
@@ -243,21 +245,24 @@ class APlayer {
}); });
// click music list: change music // click music list: change music
if (this.multiple) { const list = this.element.getElementsByClassName('aplayer-list')[0];
const listItem = this.element.getElementsByClassName('aplayer-list')[0].getElementsByTagName('li'); list.addEventListener('click', (e) => {
for (let i = 0; i < this.option.music.length; i++) { let target;
listItem[i].addEventListener('click', () => { if (e.target.tagName.toUpperCase() === 'LI') {
const musicIndex = parseInt(listItem[i].getElementsByClassName('aplayer-list-index')[0].innerHTML) - 1; target = e.target;
if (musicIndex !== this.playIndex) {
this.setMusic(musicIndex);
this.play();
}
else {
this.toggle();
}
});
} }
} else {
target = e.target.parentElement;
}
const musicIndex = parseInt(target.getElementsByClassName('aplayer-list-index')[0].innerHTML) - 1;
if (musicIndex !== this.playIndex) {
this.setMusic(musicIndex);
this.play();
}
else {
this.toggle();
}
});
// control play progress // control play progress
bar.playedBar = this.element.getElementsByClassName('aplayer-played')[0]; bar.playedBar = this.element.getElementsByClassName('aplayer-played')[0];
@@ -268,9 +273,14 @@ class APlayer {
const e = event || window.event; const e = event || window.event;
barWidth = bar.barWrap.clientWidth; barWidth = bar.barWrap.clientWidth;
const percentage = (e.clientX - getElementViewLeft(bar.barWrap)) / barWidth; const percentage = (e.clientX - getElementViewLeft(bar.barWrap)) / barWidth;
this.updateBar('played', percentage, 'width'); if (isNaN(this.audio.duration)) {
this.element.getElementsByClassName('aplayer-ptime')[0].innerHTML = this.secondToTime(percentage * this.audio.duration); this.updateBar('played', 0, 'width');
this.audio.currentTime = parseFloat(bar.playedBar.style.width) / 100 * this.audio.duration; }
else {
this.updateBar('played', percentage, 'width');
this.element.getElementsByClassName('aplayer-ptime')[0].innerHTML = this.secondToTime(percentage * this.audio.duration);
this.audio.currentTime = parseFloat(bar.playedBar.style.width) / 100 * this.audio.duration;
}
}); });
thumb.addEventListener('mouseover', () => { thumb.addEventListener('mouseover', () => {
@@ -295,15 +305,20 @@ class APlayer {
const thumbUp = () => { const thumbUp = () => {
document.removeEventListener('mouseup', thumbUp); document.removeEventListener('mouseup', thumbUp);
document.removeEventListener('mousemove', thumbMove); document.removeEventListener('mousemove', thumbMove);
this.audio.currentTime = parseFloat(bar.playedBar.style.width) / 100 * this.audio.duration; if (isNaN(this.audio.duration)) {
this.playedTime = setInterval(() => { this.updateBar('played', 0, 'width');
this.updateBar('played', this.audio.currentTime / this.audio.duration, 'width'); }
if (this.option.showlrc) { else {
this.updateLrc(); this.audio.currentTime = parseFloat(bar.playedBar.style.width) / 100 * this.audio.duration;
} this.playedTime = setInterval(() => {
this.element.getElementsByClassName('aplayer-ptime')[0].innerHTML = this.secondToTime(this.audio.currentTime); this.updateBar('played', this.audio.currentTime / this.audio.duration, 'width');
this.trigger('playing'); if (this.option.showlrc) {
}, 100); this.updateLrc();
}
this.element.getElementsByClassName('aplayer-ptime')[0].innerHTML = this.secondToTime(this.audio.currentTime);
this.trigger('playing');
}, 100);
}
}; };
thumb.addEventListener('mousedown', () => { thumb.addEventListener('mousedown', () => {
@@ -402,18 +417,15 @@ class APlayer {
}); });
// toggle menu control // toggle menu control
if (this.multiple) { list.style.height = list.offsetHeight + 'px';
const list = this.element.getElementsByClassName('aplayer-list')[0]; this.element.getElementsByClassName('aplayer-icon-menu')[0].addEventListener('click', () => {
list.style.height = list.offsetHeight + 'px'; if (!list.classList.contains('aplayer-list-hide')) {
this.element.getElementsByClassName('aplayer-icon-menu')[0].addEventListener('click', () => { list.classList.add('aplayer-list-hide');
if (!list.classList.contains('aplayer-list-hide')) { }
list.classList.add('aplayer-list-hide'); else {
} list.classList.remove('aplayer-list-hide');
else { }
list.classList.remove('aplayer-list-hide'); });
}
});
}
if (this.mode === 'random') { if (this.mode === 'random') {
this.setMusic(this.randomOrder[0]); this.setMusic(this.randomOrder[0]);
@@ -430,11 +442,11 @@ class APlayer {
*/ */
setMusic(index) { setMusic(index) {
// get this.music // get this.music
if (this.multiple && typeof(index) !== 'undefined') { if (typeof(index) !== 'undefined') {
this.playIndex = index; this.playIndex = index;
} }
const indexMusic = this.playIndex; const indexMusic = this.playIndex;
this.music = this.multiple ? this.option.music[indexMusic] : this.option.music; this.music = this.option.music[indexMusic];
// set html // set html
if (this.music.pic) { if (this.music.pic) {
@@ -442,12 +454,10 @@ class APlayer {
} }
this.element.getElementsByClassName('aplayer-title')[0].innerHTML = this.music.title; this.element.getElementsByClassName('aplayer-title')[0].innerHTML = this.music.title;
this.element.getElementsByClassName('aplayer-author')[0].innerHTML = ` - ${this.music.author}`; this.element.getElementsByClassName('aplayer-author')[0].innerHTML = ` - ${this.music.author}`;
if (this.multiple) { if (this.element.getElementsByClassName('aplayer-list-light')[0]) {
if (this.element.getElementsByClassName('aplayer-list-light')[0]) { this.element.getElementsByClassName('aplayer-list-light')[0].classList.remove('aplayer-list-light');
this.element.getElementsByClassName('aplayer-list-light')[0].classList.remove('aplayer-list-light');
}
this.element.getElementsByClassName('aplayer-list')[0].getElementsByTagName('li')[indexMusic].classList.add('aplayer-list-light');
} }
this.element.getElementsByClassName('aplayer-list')[0].getElementsByTagName('li')[indexMusic].classList.add('aplayer-list-light');
// set the previous audio object // set the previous audio object
if (this.audio) { if (this.audio) {
@@ -455,22 +465,64 @@ class APlayer {
this.audio.currentTime = 0; this.audio.currentTime = 0;
} }
if (this.multiple) { this.element.getElementsByClassName('aplayer-list')[0].scrollTop = indexMusic * 33;
this.element.getElementsByClassName('aplayer-list')[0].scrollTop = indexMusic * 33;
}
// get this audio object // get this audio object
if ((this.multiple && !this.audios[indexMusic]) || this.playIndex === -1) { if (!this.audios[indexMusic]) {
this.audio = document.createElement("audio"); this.audio = document.createElement("audio");
this.audio.src = this.music.url; this.audio.src = this.music.url;
this.audio.preload = this.option.preload ? this.option.preload : 'auto'; this.audio.preload = this.option.preload ? this.option.preload : 'auto';
this.audio.addEventListener('play', () => { this.audio.addEventListener('play', () => {
this.play(); if (this.button.classList.contains('aplayer-play')) {
this.button.classList.remove('aplayer-play');
this.button.classList.add('aplayer-pause');
this.button.innerHTML = '';
setTimeout(() => {
this.button.innerHTML = `
<button type="button" class="aplayer-icon aplayer-icon-pause">`
+ this.getSVG('pause')
+ ` </button>`;
}, 100);
// pause other players (Thanks @Aprikyblue)
if (this.option.mutex) {
for (let i = 0; i < instances.length; i++) {
if (this != instances[i]) {
instances[i].pause();
}
}
}
if (this.playedTime) {
clearInterval(this.playedTime);
}
this.playedTime = setInterval(() => {
this.updateBar('played', this.audio.currentTime / this.audio.duration, 'width');
if (this.option.showlrc) {
this.updateLrc();
}
this.ptime.innerHTML = this.secondToTime(this.audio.currentTime);
this.trigger('playing');
}, 100);
this.trigger('play');
}
}); });
this.audio.addEventListener('pause', () => { this.audio.addEventListener('pause', () => {
this.pause(); if (this.button && (this.button.classList.contains('aplayer-pause') || this.ended)) {
this.ended = false;
this.button.classList.remove('aplayer-pause');
this.button.classList.add('aplayer-play');
this.button.innerHTML = '';
setTimeout(() => {
this.button.innerHTML = `
<button type="button" class="aplayer-icon aplayer-icon-play">`
+ this.getSVG('play')
+ ` </button>`;
}, 100);
clearInterval(this.playedTime);
this.trigger('pause');
}
}); });
// show audio time: the metadata has loaded or changed // show audio time: the metadata has loaded or changed
@@ -499,8 +551,8 @@ class APlayer {
// multiple music play // multiple music play
this.ended = false; this.ended = false;
if (this.multiple) { this.audio.addEventListener('ended', () => {
this.audio.addEventListener('ended', () => { if (this.multiple) {
if (this.isMobile) { if (this.isMobile) {
this.ended = true; this.ended = true;
this.pause(); this.pause();
@@ -532,17 +584,15 @@ class APlayer {
} }
} }
} }
}); }
} else {
else {
this.audio.addEventListener('ended', () => {
if (this.mode === 'order') { if (this.mode === 'order') {
this.ended = true; this.ended = true;
this.pause(); this.pause();
this.trigger('ended'); this.trigger('ended');
} }
}); }
} });
// control volume // control volume
this.audio.volume = parseInt(this.element.getElementsByClassName('aplayer-volume')[0].style.height) / 100; this.audio.volume = parseInt(this.element.getElementsByClassName('aplayer-volume')[0].style.height) / 100;
@@ -550,9 +600,7 @@ class APlayer {
// loop // loop
this.audio.loop = !(this.multiple || this.mode === 'order'); this.audio.loop = !(this.multiple || this.mode === 'order');
if (this.multiple) { this.audios[indexMusic] = this.audio;
this.audios[indexMusic] = this.audio;
}
} }
else { else {
this.audio = this.audios[indexMusic]; this.audio = this.audios[indexMusic];
@@ -597,17 +645,12 @@ class APlayer {
// fill in lrc // fill in lrc
if (this.option.showlrc) { if (this.option.showlrc) {
const index = this.multiple ? indexMusic : 0; const index = indexMusic;
if (!this.lrcs[index]) { if (!this.lrcs[index]) {
let lrcs = ''; let lrcs = '';
if (this.option.showlrc === 1) { if (this.option.showlrc === 1) {
if (this.multiple) { lrcs = this.option.music[index].lrc;
lrcs = this.option.music[index].lrc;
}
else {
lrcs = this.option.music.lrc;
}
} }
else if (this.option.showlrc === 2 || this.option.showlrc === true) { else if (this.option.showlrc === 2 || this.option.showlrc === true) {
lrcs = this.savelrc[index]; lrcs = this.savelrc[index];
@@ -619,32 +662,28 @@ class APlayer {
if (xhr.status >= 200 && xhr.status < 300 || xhr.status === 304) { if (xhr.status >= 200 && xhr.status < 300 || xhr.status === 304) {
lrcs = xhr.responseText; lrcs = xhr.responseText;
this.lrcs[index] = parseLrc(lrcs); this.lrcs[index] = parseLrc(lrcs);
this.lrc = this.lrcs[index];
let lrcHTML = '';
this.lrcContents = this.element.getElementsByClassName('aplayer-lrc-contents')[0];
for (let i = 0; i < this.lrc.length; i++) {
lrcHTML += `<p>${this.lrc[i][1]}</p>`;
}
this.lrcContents.innerHTML = lrcHTML;
if (!this.lrcIndex) {
this.lrcIndex = 0;
}
this.lrcContents.getElementsByTagName('p')[0].classList.add('aplayer-lrc-current');
this.lrcContents.style.transform = 'translateY(0px)';
this.lrcContents.style.webkitTransform = 'translateY(0px)';
} }
else { else {
console.log('Request was unsuccessful: ' + xhr.status); console.log('Request was unsuccessful: ' + xhr.status);
this.lrcs[index] = [['00:00', 'Not available']];
} }
this.lrc = this.lrcs[index];
let lrcHTML = '';
this.lrcContents = this.element.getElementsByClassName('aplayer-lrc-contents')[0];
for (let i = 0; i < this.lrc.length; i++) {
lrcHTML += `<p>${this.lrc[i][1]}</p>`;
}
this.lrcContents.innerHTML = lrcHTML;
if (!this.lrcIndex) {
this.lrcIndex = 0;
}
this.lrcContents.getElementsByTagName('p')[0].classList.add('aplayer-lrc-current');
this.lrcContents.style.transform = 'translateY(0px)';
this.lrcContents.style.webkitTransform = 'translateY(0px)';
} }
}; };
let apiurl; let apiurl;
if (this.multiple) { apiurl = this.option.music[index].lrc;
apiurl = this.option.music[index].lrc;
}
else {
apiurl = this.option.music.lrc;
}
xhr.open('get', apiurl, true); xhr.open('get', apiurl, true);
xhr.send(null); xhr.send(null);
} }
@@ -652,7 +691,12 @@ class APlayer {
this.lrcs[index] = parseLrc(lrcs); this.lrcs[index] = parseLrc(lrcs);
} }
else { else {
this.lrcs[index] = [['00:00', 'Loading']]; if (this.option.showlrc === 3) {
this.lrcs[index] = [['00:00', 'Loading']];
}
else {
this.lrcs[index] = [['00:00', 'Not available']];
}
} }
} }
@@ -694,38 +738,8 @@ class APlayer {
if (Object.prototype.toString.call(time) === '[object Number]') { if (Object.prototype.toString.call(time) === '[object Number]') {
this.audio.currentTime = time; this.audio.currentTime = time;
} }
if (this.button.classList.contains('aplayer-play')) { if (this.audio.paused) {
this.button.classList.remove('aplayer-play');
this.button.classList.add('aplayer-pause');
this.button.innerHTML = '';
setTimeout(() => {
this.button.innerHTML = `
<button class="aplayer-icon aplayer-icon-pause">`
+ this.getSVG('pause')
+ ` </button>`;
}, 100);
// pause other players (Thanks @Aprikyblue)
if (this.option.mutex) {
for (let i = 0; i < instances.length; i++) {
if (this != instances[i]) {
instances[i].pause();
}
}
}
this.audio.play(); this.audio.play();
if (this.playedTime) {
clearInterval(this.playedTime);
}
this.playedTime = setInterval(() => {
this.updateBar('played', this.audio.currentTime / this.audio.duration, 'width');
if (this.option.showlrc) {
this.updateLrc();
}
this.ptime.innerHTML = this.secondToTime(this.audio.currentTime);
this.trigger('playing');
}, 100);
this.trigger('play');
} }
} }
@@ -733,20 +747,8 @@ class APlayer {
* Pause music * Pause music
*/ */
pause() { pause() {
if (this.button.classList.contains('aplayer-pause') || this.ended) { if (!this.audio.paused) {
this.ended = false;
this.button.classList.remove('aplayer-pause');
this.button.classList.add('aplayer-play');
this.button.innerHTML = '';
setTimeout(() => {
this.button.innerHTML = `
<button class="aplayer-icon aplayer-icon-play">`
+ this.getSVG('play')
+ ` </button>`;
}, 100);
this.audio.pause(); this.audio.pause();
clearInterval(this.playedTime);
this.trigger('pause');
} }
} }
@@ -812,13 +814,9 @@ class APlayer {
return shuffled; return shuffled;
} }
if (this.multiple) { if (this.multiple) {
if (!this.normalOrder) { this.randomOrder = shuffle([...Array(this.option.music.length)].map(function(item, i) {
this.normalOrder = []; return i;
for (let i = 0; i < this.option.music.length; i++) { }));
this.normalOrder[i] = i;
}
}
this.randomOrder = shuffle(this.normalOrder);
} }
} }
@@ -839,6 +837,55 @@ class APlayer {
return 0; return 0;
} }
} }
/**
* destroy this player
*/
destroy() {
instances.splice(instances.indexOf(this), 1);
this.pause();
this.element.innerHTML = '';
clearInterval(this.playedTime);
for (let key in this) {
if (this.hasOwnProperty(key)) {
delete this[key];
}
}
}
/**
* add music dynamically
*
* @param {Array} newMusic
*/
addMusic(newMusic) {
this.option.music = this.option.music.concat(newMusic);
const list = this.element.getElementsByClassName('aplayer-list')[0];
const listEle = list.getElementsByTagName('ol')[0];
let newItemHTML = ``;
for (let i = 0; i < newMusic.length; i++) {
newItemHTML += `
<li>
<span class="aplayer-list-cur" style="background: ${this.option.theme};"></span>
<span class="aplayer-list-index">${this.option.music.length - newMusic.length + i + 1}</span>
<span class="aplayer-list-title">${newMusic[i].title}</span>
<span class="aplayer-list-author">${newMusic[i].author}</span>
</li>`
}
listEle.innerHTML += newItemHTML;
if (!this.multiple) {
this.multiple = true;
this.element.classList.add('aplayer-withlist');
this.audio.loop = false;
}
list.style.height = 'auto';
list.style.height = list.offsetHeight + 'px';
this.getRandomOrder();
}
} }
module.exports = APlayer; module.exports = APlayer;
+16 -2
View File
@@ -34,11 +34,19 @@ $aplayer-height-lrc: $aplayer-height + $lrc-height - 6;
} }
} }
.aplayer-list { .aplayer-withlist {
&.aplayer { &.aplayer {
.aplayer-info { .aplayer-info {
border-bottom: 1px solid #e9e9e9; border-bottom: 1px solid #e9e9e9;
} }
.aplayer-list {
display: block;
}
.aplayer-icon-menu {
display: inline !important;
}
} }
} }
@@ -245,6 +253,10 @@ $aplayer-height-lrc: $aplayer-height + $lrc-height - 6;
fill: #000; fill: #000;
} }
} }
&.aplayer-icon-menu {
display: none;
}
} }
&.aplayer-time-narrow { &.aplayer-time-narrow {
@@ -302,7 +314,6 @@ $aplayer-height-lrc: $aplayer-height + $lrc-height - 6;
display: none; display: none;
position: relative; position: relative;
height: $lrc-height; height: $lrc-height;
background: #fff;
text-align: center; text-align: center;
overflow: hidden; overflow: hidden;
margin: -10px 0 7px; margin: -10px 0 7px;
@@ -350,6 +361,8 @@ $aplayer-height-lrc: $aplayer-height + $lrc-height - 6;
&.aplayer-lrc-current { &.aplayer-lrc-current {
opacity: 1; opacity: 1;
overflow: visible;
height: initial !important;
} }
} }
@@ -365,6 +378,7 @@ $aplayer-height-lrc: $aplayer-height + $lrc-height - 6;
overflow: auto; overflow: auto;
transition: all 0.5s ease; transition: all 0.5s ease;
will-change: height; will-change: height;
display: none;
&.aplayer-list-hide { &.aplayer-list-hide {
height: 0 !important; height: 0 !important;
+27 -23
View File
@@ -2,60 +2,64 @@ var webpack = require('webpack');
var path = require('path'); var path = require('path');
var autoprefixer = require('autoprefixer'); var autoprefixer = require('autoprefixer');
var libraryName = 'APlayer';
var env = process.env.WEBPACK_ENV;
var ROOT_PATH = path.resolve(__dirname); var ROOT_PATH = path.resolve(__dirname);
var APP_PATH = path.resolve(ROOT_PATH, 'src'); var APP_PATH = path.resolve(ROOT_PATH, 'src');
var BUILD_PATH = path.resolve(ROOT_PATH, 'dist'); var BUILD_PATH = path.resolve(ROOT_PATH, 'dist');
module.exports = { var plugins = [];
devtool: 'source-map', if (env !== 'dev') {
plugins.push(
new webpack.optimize.UglifyJsPlugin({
sourceMap: true
})
);
}
entry: './src/APlayer.js', module.exports = {
entry: './src/' + libraryName + '.js',
output: { output: {
path: BUILD_PATH, path: BUILD_PATH,
filename: 'APlayer.min.js', filename: libraryName + '.min.js',
library: 'APlayer', library: libraryName,
libraryTarget: 'umd', libraryTarget: 'umd',
umdNamedDefine: true umdNamedDefine: true
}, },
devtool: 'source-map',
devServer: { devServer: {
publicPath: "/dist/", publicPath: "/dist/",
}, },
module: { module: {
loaders: [ rules: [
{ {
test: /\.js$/, test: /\.js$/,
loader: 'babel', loader: 'babel-loader',
include: APP_PATH, include: APP_PATH,
query: { options: {
presets: ['es2015'] presets: ['es2015']
} }
}, },
{ {
test: /\.scss$/, test: /\.scss$/,
loaders: ['style', 'css', 'postcss', 'sass'], use: [
'style-loader',
'css-loader',
'postcss-loader',
'sass-loader'
],
include: APP_PATH include: APP_PATH
}, },
{ {
test: /\.(png|jpg)$/, test: /\.(png|jpg)$/,
loader: 'url?limit=40000' loader: 'url-loader?limit=40000'
} }
] ]
}, },
plugins: [ plugins: plugins
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
}
})
],
postcss: [
autoprefixer({
browsers: ['last 15 versions', '> 1%', 'ie 8', 'ie 7']
})
]
}; };
+3805
View File
File diff suppressed because it is too large Load Diff