From 20e2bd3e3d51c84153ea10173146a61c6cb63615 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Tue, 20 Mar 2018 23:33:59 +0800 Subject: [PATCH] support empty option.audio --- src/js/list.js | 49 +++++++++++++++++++++++++---------------- src/js/options.js | 2 +- src/js/player.js | 2 +- src/js/template.js | 14 +++++++----- src/template/player.art | 2 +- 5 files changed, 41 insertions(+), 28 deletions(-) diff --git a/src/js/list.js b/src/js/list.js index 8a943db..133113a 100644 --- a/src/js/list.js +++ b/src/js/list.js @@ -52,6 +52,7 @@ class List { add (audios) { this.player.events.trigger('listadd', audios); const wasSingle = !(this.audios.length > 1); + const wasEmpty = this.audios.length === 0; this.player.template.listOl.innerHTML += tplListItem({ theme: this.player.options.theme, @@ -71,6 +72,15 @@ class List { this.player.template.listCurs = this.player.container.querySelectorAll('.aplayer-list-cur'); this.player.template.listCurs[this.audios.length - 1].style.backgroundColor = audios.theme || this.player.options.theme; + + if (wasEmpty) { + if (this.player.options.order === 'random') { + this.switch(this.player.randomOrder[0]); + } + else { + this.switch(0); + } + } } remove (index) { @@ -113,33 +123,34 @@ class List { switch (index) { this.player.events.trigger('listswitch', index); - if (typeof index !== 'undefined') { + + if (typeof index !== 'undefined' && this.audios[index]) { this.index = index; - } - const audio = this.audios[this.index]; + const audio = this.audios[this.index]; - // set html - this.player.template.pic.style.backgroundImage = audio.cover ? `url('${audio.cover}')` : ''; - this.player.theme(this.audios[this.index].theme || this.player.options.theme, this.index, false); - this.player.template.title.innerHTML = audio.name; - this.player.template.author.innerHTML = audio.artist ? ' - ' + audio.artist : ''; + // set html + this.player.template.pic.style.backgroundImage = audio.cover ? `url('${audio.cover}')` : ''; + this.player.theme(this.audios[this.index].theme || this.player.options.theme, this.index, false); + this.player.template.title.innerHTML = audio.name; + this.player.template.author.innerHTML = audio.artist ? ' - ' + audio.artist : ''; - const light = this.player.container.getElementsByClassName('aplayer-list-light')[0]; - if (light) { - light.classList.remove('aplayer-list-light'); - } - this.player.container.querySelectorAll('.aplayer-list li')[this.index].classList.add('aplayer-list-light'); + const light = this.player.container.getElementsByClassName('aplayer-list-light')[0]; + if (light) { + light.classList.remove('aplayer-list-light'); + } + this.player.container.querySelectorAll('.aplayer-list li')[this.index].classList.add('aplayer-list-light'); - this.player.template.list.scrollTop = this.index * 33; + this.player.template.list.scrollTop = this.index * 33; - this.player.setAudio(audio); + this.player.setAudio(audio); - this.player.lrc && this.player.lrc.switch(this.index); + this.player.lrc && this.player.lrc.switch(this.index); - // set duration time - if (this.player.duration !== 1) { // compatibility: Android browsers will output 1 at first - this.player.template.dtime.innerHTML = utils.secondToTime(this.player.duration); + // set duration time + if (this.player.duration !== 1) { // compatibility: Android browsers will output 1 at first + this.player.template.dtime.innerHTML = utils.secondToTime(this.player.duration); + } } } diff --git a/src/js/options.js b/src/js/options.js index b39a8e7..2bda39f 100644 --- a/src/js/options.js +++ b/src/js/options.js @@ -14,7 +14,7 @@ export default (options) => { volume: 0.7, listFolded: false, listMaxHeight: options.listmaxheight, - audio: options.music, + audio: options.music || [], storageName: 'aplayer-setting' }; for (const defaultKey in defaultOption) { diff --git a/src/js/player.js b/src/js/player.js index 36c8972..85cdb3b 100644 --- a/src/js/player.js +++ b/src/js/player.js @@ -296,7 +296,7 @@ class APlayer { const playPromise = this.audio.play(); if (playPromise) { playPromise.catch((e) => { - console.error(e); + console.warn(e); if (e.name === 'NotAllowedError' || e.name === 'NotSupportedError') { this.setUIPaused(); diff --git a/src/js/template.js b/src/js/template.js index 7521127..e379f6f 100644 --- a/src/js/template.js +++ b/src/js/template.js @@ -10,12 +10,14 @@ class Template { } init () { - let cover; - if (this.options.order === 'random') { - cover = this.options.audio[this.randomOrder[0]].cover; - } - else { - cover = this.options.audio[0].cover; + let cover = ''; + if (this.options.audio.length) { + if (this.options.order === 'random') { + cover = this.options.audio[this.randomOrder[0]].cover; + } + else { + cover = this.options.audio[0].cover; + } } this.container.innerHTML = tplPlayer({ diff --git a/src/template/player.art b/src/template/player.art index 5a9a2a3..c4c2447 100644 --- a/src/template/player.art +++ b/src/template/player.art @@ -4,7 +4,7 @@
- + No audio