fixed player

This commit is contained in:
DIYgod
2018-03-22 01:34:24 +08:00
parent 20e2bd3e3d
commit a002a4b180
16 changed files with 383 additions and 60 deletions
+22 -4
View File
@@ -13,6 +13,8 @@ class Controller {
if (!utils.isMobile) {
this.initVolumeButton();
}
this.initMiniSwitcher();
this.initSkipButton();
}
initPlayButton () {
@@ -64,7 +66,7 @@ class Controller {
});
const thumbMove = (e) => {
let percentage = 1 - ((e.clientY || e.changedTouches[0].clientY) - utils.getElementViewTop(this.player.template.volumeBar)) / this.player.template.volumeBar.clientHeight;
let percentage = 1 - ((e.clientY || e.changedTouches[0].clientY) - utils.getElementViewTop(this.player.template.volumeBar, this.player.options.fixed)) / this.player.template.volumeBar.clientHeight;
percentage = Math.max(percentage, 0);
percentage = Math.min(percentage, 1);
this.player.volume(percentage);
@@ -74,7 +76,7 @@ class Controller {
this.player.template.volumeBarWrap.classList.remove('aplayer-volume-bar-wrap-active');
document.removeEventListener(utils.nameMap.dragEnd, thumbUp);
document.removeEventListener(utils.nameMap.dragMove, thumbMove);
let percentage = 1 - ((e.clientY || e.changedTouches[0].clientY) - utils.getElementViewTop(this.player.template.volumeBar)) / this.player.template.volumeBar.clientHeight;
let percentage = 1 - ((e.clientY || e.changedTouches[0].clientY) - utils.getElementViewTop(this.player.template.volumeBar, this.player.options.fixed)) / this.player.template.volumeBar.clientHeight;
percentage = Math.max(percentage, 0);
percentage = Math.min(percentage, 1);
this.player.volume(percentage);
@@ -130,12 +132,28 @@ class Controller {
}
initMenuButton () {
this.player.template.list.style.height = 33 * this.player.options.audio.length - 1 + 'px';
this.player.template.listOl.style.height = 33 * this.player.options.audio.length - 1 + 'px';
this.player.template.menu.addEventListener('click', () => {
this.player.list.toggle();
});
}
initMiniSwitcher () {
this.player.template.miniSwitcher.addEventListener('click', () => {
this.player.setMode(this.player.mode === 'mini' ? 'normal' : 'mini');
});
}
initSkipButton () {
this.player.template.skipBackButton.addEventListener('click', () => {
this.player.skipBack();
});
this.player.template.skipForwardButton.addEventListener('click', () => {
this.player.skipForward();
});
this.player.template.skipPlayButton.addEventListener('click', () => {
this.player.toggle();
});
}
}
export default Controller;
+4
View File
@@ -10,6 +10,8 @@ import loopAll from '../assets/loop-all.svg';
import loopOne from '../assets/loop-one.svg';
import loopNone from '../assets/loop-none.svg';
import loading from '../assets/loading.svg';
import right from '../assets/right.svg';
import skip from '../assets/skip.svg';
const Icons = {
play: play,
@@ -24,6 +26,8 @@ const Icons = {
loopOne: loopOne,
loopNone: loopNone,
loading: loading,
right: right,
skip: skip,
};
export default Icons;
+4 -5
View File
@@ -1,5 +1,6 @@
import tplListItem from '../template/list-item.art';
import utils from './utils';
import smoothScroll from 'smoothscroll';
class List {
constructor (player) {
@@ -33,6 +34,7 @@ class List {
show () {
this.player.events.trigger('listshow');
this.player.template.list.classList.remove('aplayer-list-hide');
this.player.template.listOl.scrollTop = this.index * 33;
}
hide () {
@@ -65,8 +67,6 @@ class List {
if (wasSingle && this.audios.length > 1) {
this.player.container.classList.add('aplayer-withlist');
}
this.player.template.list.style.height = this.audios.length * 33 - 1 + 'px';
this.player.template.listOl.style.height = this.audios.length * 33 - 1 + 'px';
this.player.randomOrder = utils.randomOrder(this.audios.length);
this.player.template.listCurs = this.player.container.querySelectorAll('.aplayer-list-cur');
@@ -110,8 +110,6 @@ class List {
if (this.audios.length === 1) {
this.player.container.classList.remove('aplayer-withlist');
}
this.player.template.list.style.height = this.audios.length * 33 - 1 + 'px';
this.player.template.listOl.style.height = this.audios.length * 33 - 1 + 'px';
this.player.template.listCurs = this.player.container.querySelectorAll('.aplayer-list-cur');
}
@@ -141,11 +139,12 @@ class List {
}
this.player.container.querySelectorAll('.aplayer-list li')[this.index].classList.add('aplayer-list-light');
this.player.template.list.scrollTop = this.index * 33;
smoothScroll(this.index * 33, 500, null, this.player.template.listOl);
this.player.setAudio(audio);
this.player.lrc && this.player.lrc.switch(this.index);
this.player.lrc && this.player.lrc.update(0);
// set duration time
if (this.player.duration !== 1) { // compatibility: Android browsers will output 1 at first
+2 -1
View File
@@ -83,7 +83,7 @@ class Lrc {
if (lrc_s) {
lrc_s = lrc_s.replace(/([^\]^\n])\[/g, (match, p1) => p1 + '\n[');
const lyric = lrc_s.split('\n');
const lrc = [];
let lrc = [];
const lyricLen = lyric.length;
for (let i = 0; i < lyricLen; i++) {
// match lrc time
@@ -105,6 +105,7 @@ class Lrc {
}
}
// sort by time
lrc = lrc.filter((item) => item[1]);
lrc.sort((a, b) => a[0] - b[0]);
return lrc;
}
+4 -3
View File
@@ -3,7 +3,8 @@ export default (options) => {
// default options
const defaultOption = {
container: options.element || document.getElementsByClassName('aplayer')[0],
mini: options.narrow || false,
mini: options.narrow || options.fixed || false,
fixed: false,
autoplay: false,
mutex: true,
lrcType: options.showlrc || options.lrc || 0,
@@ -12,8 +13,8 @@ export default (options) => {
loop: 'all',
order: 'list',
volume: 0.7,
listFolded: false,
listMaxHeight: options.listmaxheight,
listFolded: options.fixed,
listMaxHeight: options.listmaxheight || '250px',
audio: options.music || [],
storageName: 'aplayer-setting'
};
+74 -34
View File
@@ -32,7 +32,7 @@ class APlayer {
this.randomOrder = utils.randomOrder(this.options.audio.length);
this.container.classList.add('aplayer');
if (this.options.lrcType) {
if (this.options.lrcType && !this.options.fixed) {
this.container.classList.add('aplayer-withlrc');
}
if (this.options.audio.length > 1) {
@@ -45,9 +45,6 @@ class APlayer {
if (this.arrow) {
this.container.classList.add('aplayer-arrow');
}
if (this.options.mini) {
this.setMode('mini');
}
// save lrc
this.container = this.options.container;
@@ -66,6 +63,14 @@ class APlayer {
randomOrder: this.randomOrder,
});
if (this.options.fixed) {
this.container.classList.add('aplayer-fixed');
this.template.body.style.width = this.template.body.offsetWidth - 18 + 'px';
}
if (this.options.mini) {
this.setMode('mini');
this.template.info.style.display = 'block';
}
if (this.template.info.offsetWidth < 200) {
this.template.time.classList.add('aplayer-time-narrow');
}
@@ -150,6 +155,15 @@ class APlayer {
// audio download error: an error occurs
this.on('error', () => {
if (this.list.audios.length) {
this.notice('An audio error has occurred, player will skip forward in 2 seconds.');
setTimeout(() => {
this.skipForward();
if (!this.paused) {
this.play();
}
}, 2000);
}
else {
this.notice('An audio error has occurred.');
}
});
@@ -169,11 +183,11 @@ class APlayer {
}
else if (this.options.order === 'random') {
if (this.randomOrder.indexOf(this.list.index) < this.randomOrder.length - 1) {
this.list.switch(this.nextRandomNum());
this.list.switch(this.nextIndex());
this.play();
}
else {
this.list.switch(this.nextRandomNum());
this.list.switch(this.nextIndex());
this.pause();
}
}
@@ -183,12 +197,7 @@ class APlayer {
this.play();
}
else if (this.options.loop === 'all') {
if (this.options.order === 'list') {
this.list.switch((this.list.index + 1) % this.list.audios.length);
}
else if (this.options.order === 'random') {
this.list.switch(this.nextRandomNum());
}
this.skipForward();
this.play();
}
});
@@ -277,6 +286,7 @@ class APlayer {
setTimeout(() => {
this.template.button.innerHTML = Icons.pause;
}, 100);
this.template.skipPlayButton.innerHTML = Icons.pause;
}
this.timer.enable('loading');
@@ -297,8 +307,7 @@ class APlayer {
if (playPromise) {
playPromise.catch((e) => {
console.warn(e);
if (e.name === 'NotAllowedError' ||
e.name === 'NotSupportedError') {
if (e.name === 'NotAllowedError') {
this.setUIPaused();
}
});
@@ -315,6 +324,7 @@ class APlayer {
setTimeout(() => {
this.template.button.innerHTML = Icons.play;
}, 100);
this.template.skipPlayButton.innerHTML = Icons.play;
}
this.container.classList.remove('aplayer-loading');
@@ -381,24 +391,6 @@ class APlayer {
}
}
/**
* get next random number
*/
nextRandomNum () {
if (this.list.audios.length > 1) {
const index = this.randomOrder.indexOf(this.list.index);
if (index === this.randomOrder.length - 1) {
return this.randomOrder[0];
}
else {
return this.randomOrder[index + 1];
}
}
else {
return 0;
}
}
// abandoned
switchAudio (index) {
this.list.switch(index);
@@ -442,15 +434,63 @@ class APlayer {
if (this.noticeTime) {
clearTimeout(this.noticeTime);
}
this.events.trigger('notice_show', text);
this.events.trigger('noticeshow', text);
if (time) {
this.noticeTime = setTimeout(() => {
this.template.notice.style.opacity = 0;
this.events.trigger('notice_hide');
this.events.trigger('noticehide');
}, time);
}
}
prevIndex () {
if (this.list.audios.length > 1) {
if (this.options.order === 'list') {
return this.list.index - 1 < 0 ? this.list.audios.length - 1 : this.list.index - 1;
}
else if (this.options.order === 'random') {
const index = this.randomOrder.indexOf(this.list.index);
if (index === 0) {
return this.randomOrder[this.randomOrder.length - 1];
}
else {
return this.randomOrder[index - 1];
}
}
}
else {
return 0;
}
}
nextIndex () {
if (this.list.audios.length > 1) {
if (this.options.order === 'list') {
return (this.list.index + 1) % this.list.audios.length;
}
else if (this.options.order === 'random') {
const index = this.randomOrder.indexOf(this.list.index);
if (index === this.randomOrder.length - 1) {
return this.randomOrder[0];
}
else {
return this.randomOrder[index + 1];
}
}
}
else {
return 0;
}
}
skipBack () {
this.list.switch(this.prevIndex());
}
skipForward () {
this.list.switch(this.nextIndex());
}
static get version () {
/* global APLAYER_VERSION */
return APLAYER_VERSION;
+5
View File
@@ -33,6 +33,7 @@ class Template {
this.time = this.container.querySelector('.aplayer-time');
this.barWrap = this.container.querySelector('.aplayer-bar-wrap');
this.button = this.container.querySelector('.aplayer-button');
this.body = this.container.querySelector('.aplayer-body');
this.list = this.container.querySelector('.aplayer-list');
this.listOl = this.container.querySelector('.aplayer-list ol');
this.listCurs = this.container.querySelectorAll('.aplayer-list-cur');
@@ -51,6 +52,10 @@ class Template {
this.author = this.container.querySelector('.aplayer-author');
this.dtime = this.container.querySelector('.aplayer-dtime');
this.notice = this.container.querySelector('.aplayer-notice');
this.miniSwitcher = this.container.querySelector('.aplayer-miniswitcher');
this.skipBackButton = this.container.querySelector('.aplayer-icon-back');
this.skipForwardButton = this.container.querySelector('.aplayer-icon-forward');
this.skipPlayButton = this.container.querySelector('.aplayer-icon-play');
}
}
+2 -2
View File
@@ -39,7 +39,7 @@ const utils = {
return actualLeft - elementScrollLeft;
},
getElementViewTop: (element) => {
getElementViewTop: (element, noScrollTop) => {
let actualTop = element.offsetTop;
let current = element.offsetParent;
let elementScrollTop = 0;
@@ -48,7 +48,7 @@ const utils = {
current = current.offsetParent;
}
elementScrollTop = document.body.scrollTop + document.documentElement.scrollTop;
return actualTop - elementScrollTop;
return noScrollTop ? actualTop : actualTop - elementScrollTop;
},
isMobile: isMobile,