Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9f9ded6e1e | ||
|
|
12fc04957d | ||
|
|
c5776b2a78 | ||
|
|
548faa4bf3 | ||
|
|
91566de67a | ||
|
|
7b8ad10c04 | ||
|
|
47270238cb | ||
|
|
04e950b1eb | ||
|
|
54f9d5dcf9 | ||
|
|
2b6ba28167 | ||
|
|
10ad8cd38d | ||
|
|
adb2cd4ed8 | ||
|
|
f1bc748330 | ||
|
|
008552940f | ||
|
|
40d95344fd | ||
|
|
da71e49dc0 | ||
|
|
b019271309 | ||
|
|
5206eed8e5 | ||
|
|
82b7f777f6 |
@@ -7,13 +7,13 @@
|
|||||||
[](https://travis-ci.org/DIYgod/APlayer)
|
[](https://travis-ci.org/DIYgod/APlayer)
|
||||||
[](https://www.anotherhome.net/)
|
[](https://www.anotherhome.net/)
|
||||||
|
|
||||||
Wow, such a beautiful html5 music player
|
> Wow, such a beautiful html5 music player
|
||||||
|
|
||||||
## Introduction
|
## Introduction
|
||||||
|
|
||||||
UI 参考网易云音乐外链播放器
|
UI 参考网易云音乐外链播放器
|
||||||
|
|
||||||
[Demo](https://www.anotherhome.net/file/APlayer)
|
[Demo](http://aplayer.js.org)
|
||||||
|
|
||||||
Screenshot
|
Screenshot
|
||||||

|

|
||||||
@@ -57,6 +57,8 @@ var option = {
|
|||||||
showlrc: 0, // Optional, show lrc, can be 0, 1, 2, see: ###With lrc
|
showlrc: 0, // Optional, show lrc, can be 0, 1, 2, see: ###With lrc
|
||||||
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
|
||||||
|
loop: true, // Optional, loop play music, default: true
|
||||||
|
preload: 'metadata', // Optional, the way to load music, can be 'none' 'metadata' 'auto', default: 'metadata' in Desktop, 'none' in mobile
|
||||||
music: { // Required, music info, see: ###With playlist
|
music: { // Required, music info, see: ###With playlist
|
||||||
title: 'Preparation', // Required, music title
|
title: 'Preparation', // Required, music title
|
||||||
author: 'Hans Zimmer/Richard Harvey', // Required, music author
|
author: 'Hans Zimmer/Richard Harvey', // Required, music author
|
||||||
@@ -70,8 +72,11 @@ var option = {
|
|||||||
#### API
|
#### API
|
||||||
|
|
||||||
+ `ap.init()`
|
+ `ap.init()`
|
||||||
+ `ap.play()`
|
+ `ap.play()` // Resume play
|
||||||
+ `ap.pause()`
|
+ `ap.play(time)` // Set currentTime
|
||||||
|
+ `ap.pause()` // Pause
|
||||||
|
+ `ap.volume(percentage)` // Set volume
|
||||||
|
+ `ap.on(event, handler)` // Event binding
|
||||||
|
|
||||||
#### Event binding
|
#### Event binding
|
||||||
|
|
||||||
@@ -190,9 +195,11 @@ $ npm run build
|
|||||||
|
|
||||||
## Related Projects
|
## Related Projects
|
||||||
|
|
||||||
+ [APlayer-Typecho-Plugin](https://github.com/zgq354/APlayer-Typecho-Plugin)
|
- [APlayer-Typecho-Plugin](https://github.com/zgq354/APlayer-Typecho-Plugin)
|
||||||
|
|
||||||
+ [hexo-tag-aplayer](https://github.com/grzhan/hexo-tag-aplayer)
|
- [hexo-tag-aplayer](https://github.com/grzhan/hexo-tag-aplayer)
|
||||||
|
|
||||||
|
- [163music-APlayer-you-get-docker](https://github.com/YUX-IO/163music-APlayer-you-get-docker)
|
||||||
|
|
||||||
|
|
||||||
## LICENSE
|
## LICENSE
|
||||||
|
|||||||
+19
-18
File diff suppressed because one or more lines are too long
Vendored
+1
-1
File diff suppressed because one or more lines are too long
Vendored
+1
-1
File diff suppressed because one or more lines are too long
Vendored
+1
-1
File diff suppressed because one or more lines are too long
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "aplayer",
|
"name": "aplayer",
|
||||||
"version": "1.4.2",
|
"version": "1.4.7",
|
||||||
"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": {
|
||||||
|
|||||||
+86
-49
@@ -1,5 +1,4 @@
|
|||||||
(() => {
|
((instances) => {
|
||||||
let APlayers = [];
|
|
||||||
|
|
||||||
class APlayer {
|
class APlayer {
|
||||||
/**
|
/**
|
||||||
@@ -23,7 +22,8 @@
|
|||||||
autoplay: false,
|
autoplay: false,
|
||||||
mutex: true,
|
mutex: true,
|
||||||
showlrc: 0,
|
showlrc: 0,
|
||||||
theme: '#b7daff'
|
theme: '#b7daff',
|
||||||
|
loop: true
|
||||||
};
|
};
|
||||||
for (let defaultKey in defaultOption) {
|
for (let defaultKey in defaultOption) {
|
||||||
if (defaultOption.hasOwnProperty(defaultKey) && !option.hasOwnProperty(defaultKey)) {
|
if (defaultOption.hasOwnProperty(defaultKey) && !option.hasOwnProperty(defaultKey)) {
|
||||||
@@ -36,7 +36,7 @@
|
|||||||
|
|
||||||
this.option = option;
|
this.option = option;
|
||||||
this.audios = [];
|
this.audios = [];
|
||||||
this.loop = true;
|
this.loop = option.loop;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse second to 00:00 format
|
* Parse second to 00:00 format
|
||||||
@@ -127,12 +127,12 @@
|
|||||||
// define APlayer events
|
// define APlayer events
|
||||||
this.eventTypes = ['play', 'pause', 'canplay', 'playing', 'ended', 'error'];
|
this.eventTypes = ['play', 'pause', 'canplay', 'playing', 'ended', 'error'];
|
||||||
this.event = {};
|
this.event = {};
|
||||||
for (let type of this.eventTypes) {
|
for (let i = 0; i < this.eventTypes.length; i++) {
|
||||||
this.event[type] = [];
|
this.event[this.eventTypes[i]] = [];
|
||||||
}
|
}
|
||||||
this.trigger = (type) => {
|
this.trigger = (type) => {
|
||||||
for (let func of this.event[type]) {
|
for (let i = 0; i < this.event[type].length; i++) {
|
||||||
func();
|
this.event[type][i]();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -175,7 +175,7 @@
|
|||||||
|
|
||||||
// fill in HTML
|
// fill in HTML
|
||||||
let eleHTML = `
|
let eleHTML = `
|
||||||
<div class="aplayer-pic" ${(this.music.pic ? (`style="background-image: url("${encodeURI(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">
|
||||||
<i class="demo-icon aplayer-icon-play"></i>
|
<i class="demo-icon aplayer-icon-play"></i>
|
||||||
</div>
|
</div>
|
||||||
@@ -207,7 +207,7 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<i class="demo-icon aplayer-icon-loop"></i>${(this.multiple ? `<i class="demo-icon aplayer-icon-menu"></i>` : ``)}
|
<i class="demo-icon aplayer-icon-loop${(this.loop ? `` : ` aplayer-noloop`)}"></i>${(this.multiple ? `<i class="demo-icon aplayer-icon-menu"></i>` : ``)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>`;
|
</div>`;
|
||||||
@@ -230,6 +230,8 @@
|
|||||||
}
|
}
|
||||||
this.element.innerHTML = eleHTML;
|
this.element.innerHTML = eleHTML;
|
||||||
|
|
||||||
|
this.ptime = this.element.getElementsByClassName('aplayer-ptime')[0];
|
||||||
|
|
||||||
if (this.element.getElementsByClassName('aplayer-info')[0].offsetWidth < 200) {
|
if (this.element.getElementsByClassName('aplayer-info')[0].offsetWidth < 200) {
|
||||||
this.element.getElementsByClassName('aplayer-time')[0].classList.add('aplayer-time-narrow');
|
this.element.getElementsByClassName('aplayer-time')[0].classList.add('aplayer-time-narrow');
|
||||||
}
|
}
|
||||||
@@ -261,7 +263,12 @@
|
|||||||
if (musicIndex !== this.playIndex) {
|
if (musicIndex !== this.playIndex) {
|
||||||
this.setMusic(musicIndex);
|
this.setMusic(musicIndex);
|
||||||
}
|
}
|
||||||
this.play();
|
if (this.isMobile) {
|
||||||
|
this.pause();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this.play();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -304,7 +311,14 @@
|
|||||||
document.removeEventListener('mouseup', thumbUp);
|
document.removeEventListener('mouseup', thumbUp);
|
||||||
document.removeEventListener('mousemove', thumbMove);
|
document.removeEventListener('mousemove', thumbMove);
|
||||||
this.audio.currentTime = parseFloat(this.playedBar.style.width) / 100 * this.audio.duration;
|
this.audio.currentTime = parseFloat(this.playedBar.style.width) / 100 * this.audio.duration;
|
||||||
this.play();
|
this.playedTime = setInterval(() => {
|
||||||
|
this.updateBar('played', this.audio.currentTime / this.audio.duration, 'width');
|
||||||
|
if (this.option.showlrc) {
|
||||||
|
this.updateLrc();
|
||||||
|
}
|
||||||
|
this.element.getElementsByClassName('aplayer-ptime')[0].innerHTML = this.secondToTime(this.audio.currentTime);
|
||||||
|
this.trigger('playing');
|
||||||
|
}, 100);
|
||||||
};
|
};
|
||||||
|
|
||||||
this.thumb.addEventListener('mousedown', () => {
|
this.thumb.addEventListener('mousedown', () => {
|
||||||
@@ -317,34 +331,24 @@
|
|||||||
// control volume
|
// control volume
|
||||||
this.volumeBar = this.element.getElementsByClassName('aplayer-volume')[0];
|
this.volumeBar = this.element.getElementsByClassName('aplayer-volume')[0];
|
||||||
const volumeBarWrap = this.element.getElementsByClassName('aplayer-volume-bar')[0];
|
const volumeBarWrap = this.element.getElementsByClassName('aplayer-volume-bar')[0];
|
||||||
const volumeicon = this.element.getElementsByClassName('aplayer-time')[0].getElementsByTagName('i')[0];
|
this.volumeicon = this.element.getElementsByClassName('aplayer-time')[0].getElementsByTagName('i')[0];
|
||||||
const barHeight = 35;
|
const barHeight = 35;
|
||||||
this.element.getElementsByClassName('aplayer-volume-bar-wrap')[0].addEventListener('click', (event) => {
|
this.element.getElementsByClassName('aplayer-volume-bar-wrap')[0].addEventListener('click', (event) => {
|
||||||
const e = event || window.event;
|
const e = event || window.event;
|
||||||
let percentage = (barHeight - e.clientY + getElementViewTop(volumeBarWrap)) / barHeight;
|
let percentage = (barHeight - e.clientY + getElementViewTop(volumeBarWrap)) / barHeight;
|
||||||
percentage = percentage > 0 ? percentage : 0;
|
percentage = percentage > 0 ? percentage : 0;
|
||||||
percentage = percentage < 1 ? percentage : 1;
|
percentage = percentage < 1 ? percentage : 1;
|
||||||
this.updateBar('volume', percentage, 'height');
|
this.volume(percentage);
|
||||||
this.audio.volume = percentage;
|
|
||||||
if (this.audio.muted) {
|
|
||||||
this.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', () => {
|
this.volumeicon.addEventListener('click', () => {
|
||||||
if (this.audio.muted) {
|
if (this.audio.muted) {
|
||||||
this.audio.muted = false;
|
this.audio.muted = false;
|
||||||
volumeicon.className = this.audio.volume === 1 ? 'demo-icon aplayer-icon-volume-up' : 'demo-icon aplayer-icon-volume-down';
|
this.volumeicon.className = this.audio.volume === 1 ? 'demo-icon aplayer-icon-volume-up' : 'demo-icon aplayer-icon-volume-down';
|
||||||
this.updateBar('volume', this.audio.volume, 'height');
|
this.updateBar('volume', this.audio.volume, 'height');
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
this.audio.muted = true;
|
this.audio.muted = true;
|
||||||
volumeicon.className = 'demo-icon aplayer-icon-volume-off';
|
this.volumeicon.className = 'demo-icon aplayer-icon-volume-off';
|
||||||
this.updateBar('volume', 0, 'height');
|
this.updateBar('volume', 0, 'height');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -404,8 +408,8 @@
|
|||||||
|
|
||||||
this.setMusic(0);
|
this.setMusic(0);
|
||||||
|
|
||||||
APlayers.push(this);
|
instances.push(this);
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set music
|
* Set music
|
||||||
@@ -420,7 +424,7 @@
|
|||||||
|
|
||||||
// set html
|
// set html
|
||||||
if (this.music.pic) {
|
if (this.music.pic) {
|
||||||
this.element.getElementsByClassName('aplayer-pic')[0].style.backgroundImage = `url(${encodeURI(this.music.pic)})`;
|
this.element.getElementsByClassName('aplayer-pic')[0].style.backgroundImage = `url('${this.music.pic}')`;
|
||||||
}
|
}
|
||||||
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}`;
|
||||||
@@ -441,7 +445,12 @@
|
|||||||
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;
|
||||||
this.audio.preload = this.isMobile ? 'none' : 'metadata';
|
if (this.option.preload) {
|
||||||
|
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', () => {
|
||||||
@@ -471,16 +480,23 @@
|
|||||||
this.ended = false;
|
this.ended = false;
|
||||||
if (this.multiple) {
|
if (this.multiple) {
|
||||||
this.audio.addEventListener('ended', () => {
|
this.audio.addEventListener('ended', () => {
|
||||||
if (this.playIndex < this.option.music.length - 1) {
|
if (this.isMobile) {
|
||||||
this.setMusic(++this.playIndex);
|
|
||||||
}
|
|
||||||
else if (this.loop) {
|
|
||||||
this.setMusic(0);
|
|
||||||
}
|
|
||||||
else if (!this.loop) {
|
|
||||||
this.ended = true;
|
this.ended = true;
|
||||||
this.pause();
|
this.pause();
|
||||||
this.trigger('ended');
|
return;
|
||||||
|
}
|
||||||
|
if (this.audio.currentTime !== 0) {
|
||||||
|
if (this.playIndex < this.option.music.length - 1) {
|
||||||
|
this.setMusic(++this.playIndex);
|
||||||
|
}
|
||||||
|
else if (this.loop) {
|
||||||
|
this.setMusic(0);
|
||||||
|
}
|
||||||
|
else if (!this.loop) {
|
||||||
|
this.ended = true;
|
||||||
|
this.pause();
|
||||||
|
this.trigger('ended');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -541,12 +557,15 @@
|
|||||||
if (this.isMobile) {
|
if (this.isMobile) {
|
||||||
this.pause();
|
this.pause();
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Play music
|
* Play music
|
||||||
*/
|
*/
|
||||||
play() {
|
play(time) {
|
||||||
|
if (Object.prototype.toString.call(time) === '[object Number]') {
|
||||||
|
this.audio.currentTime = time;
|
||||||
|
}
|
||||||
if (this.audio.paused) {
|
if (this.audio.paused) {
|
||||||
this.button.classList.remove('aplayer-play');
|
this.button.classList.remove('aplayer-play');
|
||||||
this.button.classList.add('aplayer-pause');
|
this.button.classList.add('aplayer-pause');
|
||||||
@@ -557,12 +576,13 @@
|
|||||||
|
|
||||||
// pause other players (Thanks @Aprikyblue)
|
// pause other players (Thanks @Aprikyblue)
|
||||||
if (this.option.mutex) {
|
if (this.option.mutex) {
|
||||||
for (let i = 0; i < APlayers.length; i++) {
|
for (let i = 0; i < instances.length; i++) {
|
||||||
if (this != APlayers[i]) {
|
if (this != instances[i]) {
|
||||||
APlayers[i].pause();
|
instances[i].pause();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
console.log(Object.prototype.toString.call(time));
|
||||||
this.audio.play();
|
this.audio.play();
|
||||||
if (this.playedTime) {
|
if (this.playedTime) {
|
||||||
clearInterval(this.playedTime);
|
clearInterval(this.playedTime);
|
||||||
@@ -572,12 +592,12 @@
|
|||||||
if (this.option.showlrc) {
|
if (this.option.showlrc) {
|
||||||
this.updateLrc();
|
this.updateLrc();
|
||||||
}
|
}
|
||||||
this.element.getElementsByClassName('aplayer-ptime')[0].innerHTML = this.secondToTime(this.audio.currentTime);
|
this.ptime.innerHTML = this.secondToTime(this.audio.currentTime);
|
||||||
this.trigger('playing');
|
this.trigger('playing');
|
||||||
}, 100);
|
}, 100);
|
||||||
this.trigger('play');
|
this.trigger('play');
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Pause music
|
* Pause music
|
||||||
@@ -595,7 +615,24 @@
|
|||||||
clearInterval(this.playedTime);
|
clearInterval(this.playedTime);
|
||||||
this.trigger('pause');
|
this.trigger('pause');
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set volume
|
||||||
|
*/
|
||||||
|
volume(percentage) {
|
||||||
|
this.updateBar('volume', percentage, 'height');
|
||||||
|
this.audio.volume = percentage;
|
||||||
|
if (this.audio.muted) {
|
||||||
|
this.audio.muted = false;
|
||||||
|
}
|
||||||
|
if (percentage === 1) {
|
||||||
|
this.volumeicon.className = 'demo-icon aplayer-icon-volume-up';
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this.volumeicon.className = 'demo-icon aplayer-icon-volume-down';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* attach event
|
* attach event
|
||||||
@@ -608,9 +645,9 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
|
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
|
||||||
module.exports = APlayer
|
module.exports = APlayer;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
window.APlayer = APlayer;
|
window.APlayer = APlayer;
|
||||||
}
|
}
|
||||||
})();
|
})([]);
|
||||||
Reference in New Issue
Block a user