6 Commits
Author SHA1 Message Date
DIYgod 9f9ded6e1e API: play volume; Bug: encodeURI image link 2016-07-08 11:25:34 +08:00
DIYgod 12fc04957d new option: preload 2016-06-13 14:23:51 +08:00
DIYgod c5776b2a78 fix bugs in HTML 2016-06-13 14:06:27 +08:00
DIYgod 548faa4bf3 add related project: 163music-APlayer-you-get-docker 2016-05-15 18:32:58 +08:00
DIYgod 91566de67a new npm version 2016-05-14 22:38:42 +08:00
DIYgod 7b8ad10c04 Fix bug: multiple ended event in iOS; clicking list error in mobile 2016-05-14 22:37:48 +08:00
7 changed files with 73 additions and 38 deletions
+11 -5
View File
@@ -7,7 +7,7 @@
[![Travis](https://img.shields.io/travis/DIYgod/APlayer.svg?style=flat-square)](https://travis-ci.org/DIYgod/APlayer) [![Travis](https://img.shields.io/travis/DIYgod/APlayer.svg?style=flat-square)](https://travis-ci.org/DIYgod/APlayer)
[![%e2%9d%a4](https://img.shields.io/badge/made%20with-%e2%9d%a4-ff69b4.svg?style=flat-square)](https://www.anotherhome.net/) [![%e2%9d%a4](https://img.shields.io/badge/made%20with-%e2%9d%a4-ff69b4.svg?style=flat-square)](https://www.anotherhome.net/)
Wow, such a beautiful html5 music player > Wow, such a beautiful html5 music player
## Introduction ## Introduction
@@ -58,6 +58,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
loop: true, // Optional, loop play music, default: true 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
@@ -71,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
@@ -191,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
+1
View File
@@ -5,6 +5,7 @@ var ap1 = new APlayer({
showlrc: false, showlrc: false,
mutex: true, mutex: true,
theme: '#e6d0b2', theme: '#e6d0b2',
preload: 'metadata',
music: { music: {
title: 'Preparation', title: 'Preparation',
author: 'Hans Zimmer/Richard Harvey', author: 'Hans Zimmer/Richard Harvey',
+1 -1
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
+1 -1
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "aplayer", "name": "aplayer",
"version": "1.4.4", "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": {
+57 -29
View File
@@ -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>
@@ -263,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();
}
}); });
} }
} }
@@ -326,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');
} }
}); });
@@ -429,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}`;
@@ -450,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', () => {
@@ -480,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');
}
} }
}); });
} }
@@ -555,7 +562,10 @@
/** /**
* 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');
@@ -572,6 +582,7 @@
} }
} }
} }
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);
@@ -606,6 +617,23 @@
} }
} }
/**
* 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
*/ */
@@ -617,7 +645,7 @@
} }
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;