diff --git a/demo/demo.js b/demo/demo.js index 8a04dfe..31b1906 100644 --- a/demo/demo.js +++ b/demo/demo.js @@ -6,11 +6,11 @@ const ap1 = new APlayer({ mutex: true, theme: '#e6d0b2', preload: 'metadata', - music: { - title: 'Preparation', - author: 'Hans Zimmer/Richard Harvey', + audio: { + name: 'Preparation', + artist: 'Hans Zimmer/Richard Harvey', url: 'https://moeplayer.b0.upaiyun.com/aplayer/preparation.mp3', - pic: 'https://moeplayer.b0.upaiyun.com/aplayer/preparation.jpg' + cover: 'https://moeplayer.b0.upaiyun.com/aplayer/preparation.jpg' } }); ap1.on('play', function () { @@ -42,11 +42,11 @@ const ap2 = new APlayer({ lrc: false, mutex: true, theme: '#e6d0b2', - music: { - title: 'Preparation', - author: 'Hans Zimmer/Richard Harvey', + audio: { + name: 'Preparation', + artist: 'Hans Zimmer/Richard Harvey', url: 'https://moeplayer.b0.upaiyun.com/aplayer/preparation.mp3', - pic: 'https://moeplayer.b0.upaiyun.com/aplayer/preparation.jpg' + cover: 'https://moeplayer.b0.upaiyun.com/aplayer/preparation.jpg' } }); @@ -57,11 +57,11 @@ const ap3 = new APlayer({ lrc: 3, mutex: true, theme: '#615754', - music: { - title: '回レ!雪月花', - author: '小倉唯', + audio: { + name: '回レ!雪月花', + artist: '小倉唯', url: 'https://moeplayer.b0.upaiyun.com/aplayer/snowmoonflowers.mp3', - pic: 'https://moeplayer.b0.upaiyun.com/aplayer/snowmoonflowers.jpg', + cover: 'https://moeplayer.b0.upaiyun.com/aplayer/snowmoonflowers.jpg', lrc: "https://moeplayer.b0.upaiyun.com/aplayer/snowmoonflowers.lrc" } }); @@ -74,24 +74,24 @@ const ap4 = new APlayer({ mutex: true, theme: '#ad7a86', order: 'random', - music: [ + audio: [ { - title: 'あっちゅ~ま青春!', - author: '七森中☆ごらく部', + name: 'あっちゅ~ま青春!', + artist: '七森中☆ごらく部', url: 'https://moeplayer.b0.upaiyun.com/aplayer/yuruyuri.mp3', - pic: 'https://moeplayer.b0.upaiyun.com/aplayer/yuruyuri.jpg' + cover: 'https://moeplayer.b0.upaiyun.com/aplayer/yuruyuri.jpg' }, { - title: 'secret base~君がくれたもの~', - author: '茅野愛衣', + name: 'secret base~君がくれたもの~', + artist: '茅野愛衣', url: 'https://moeplayer.b0.upaiyun.com/aplayer/secretbase.mp3', - pic: 'https://moeplayer.b0.upaiyun.com/aplayer/secretbase.jpg' + cover: 'https://moeplayer.b0.upaiyun.com/aplayer/secretbase.jpg' }, { - title: '回レ!雪月花', - author: '小倉唯', + name: '回レ!雪月花', + artist: '小倉唯', url: 'https://moeplayer.b0.upaiyun.com/aplayer/snowmoonflowers.mp3', - pic: 'https://moeplayer.b0.upaiyun.com/aplayer/snowmoonflowers.jpg' + cover: 'https://moeplayer.b0.upaiyun.com/aplayer/snowmoonflowers.jpg' } ] }); @@ -104,26 +104,26 @@ const ap5 = new APlayer({ mutex: true, theme: '#ad7a86', listFolded: true, - music: [ + audio: [ { - title: 'あっちゅ~ま青春!', - author: '七森中☆ごらく部', + name: 'あっちゅ~ま青春!', + artist: '七森中☆ごらく部', url: 'https://moeplayer.b0.upaiyun.com/aplayer/yuruyuri.mp3', - pic: 'https://moeplayer.b0.upaiyun.com/aplayer/yuruyuri.jpg', + cover: 'https://moeplayer.b0.upaiyun.com/aplayer/yuruyuri.jpg', lrc: 'https://moeplayer.b0.upaiyun.com/aplayer/yuruyuri.lrc' }, { - title: 'secret base~君がくれたもの~', - author: '茅野愛衣', + name: 'secret base~君がくれたもの~', + artist: '茅野愛衣', url: 'https://moeplayer.b0.upaiyun.com/aplayer/secretbase.mp3', - pic: 'https://moeplayer.b0.upaiyun.com/aplayer/secretbase.jpg', + cover: 'https://moeplayer.b0.upaiyun.com/aplayer/secretbase.jpg', lrc: 'https://moeplayer.b0.upaiyun.com/aplayer/secretbase.lrc' }, { - title: '回レ!雪月花', - author: '小倉唯', + name: '回レ!雪月花', + artist: '小倉唯', url: 'https://moeplayer.b0.upaiyun.com/aplayer/snowmoonflowers.mp3', - pic: 'https://moeplayer.b0.upaiyun.com/aplayer/snowmoonflowers.jpg', + cover: 'https://moeplayer.b0.upaiyun.com/aplayer/snowmoonflowers.jpg', lrc: 'https://moeplayer.b0.upaiyun.com/aplayer/snowmoonflowers.lrc' } ] diff --git a/demo/index.html b/demo/index.html index d13ce36..5862fea 100644 --- a/demo/index.html +++ b/demo/index.html @@ -61,22 +61,22 @@ diff --git a/src/css/index.scss b/src/css/index.scss index 5591655..4bc51ad 100644 --- a/src/css/index.scss +++ b/src/css/index.scss @@ -51,6 +51,7 @@ $aplayer-height-lrc: $aplayer-height + $lrc-height - 6; } .aplayer { + background: #fff; font-family: Arial, Helvetica, sans-serif; margin: 5px; box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.07), 0 1px 5px 0 rgba(0, 0, 0, 0.1); diff --git a/src/js/controller.js b/src/js/controller.js index 74cc415..ef87c53 100644 --- a/src/js/controller.js +++ b/src/js/controller.js @@ -33,9 +33,9 @@ class Controller { else { target = e.target.parentElement; } - const musicIndex = parseInt(target.getElementsByClassName('aplayer-list-index')[0].innerHTML) - 1; - if (musicIndex !== this.player.playIndex) { - this.player.switchAudio(musicIndex); + const audioIndex = parseInt(target.getElementsByClassName('aplayer-list-index')[0].innerHTML) - 1; + if (audioIndex !== this.player.playIndex) { + this.player.switchAudio(audioIndex); this.player.play(); } else { @@ -153,7 +153,7 @@ class Controller { } initMenuButton () { - this.player.template.list.style.height = 33 * this.player.options.music.length - 1 + 'px'; + this.player.template.list.style.height = 33 * this.player.options.audio.length - 1 + 'px'; this.player.template.menu.addEventListener('click', () => { if (!this.player.template.list.classList.contains('aplayer-list-hide')) { this.player.template.list.classList.add('aplayer-list-hide'); diff --git a/src/js/options.js b/src/js/options.js index 3e757f1..aad95b1 100644 --- a/src/js/options.js +++ b/src/js/options.js @@ -17,11 +17,11 @@ export default (options) => { } } - if (Object.prototype.toString.call(options.music) !== '[object Array]') { - options.music = [options.music]; + if (Object.prototype.toString.call(options.audio) !== '[object Array]') { + options.audio = [options.audio]; } - if (options.music.length <= 1 && options.loop === 'one') { + if (options.audio.length <= 1 && options.loop === 'one') { options.loop = 'all'; } diff --git a/src/js/player.js b/src/js/player.js index 4a91b9e..90df828 100644 --- a/src/js/player.js +++ b/src/js/player.js @@ -28,13 +28,13 @@ class APlayer { this.paused = true; this.playedPromise = Promise.resolve(); - this.randomOrder = utils.randomOrder(this.options.music.length); + this.randomOrder = utils.randomOrder(this.options.audio.length); this.container.classList.add('aplayer'); if (this.options.lrc) { this.container.classList.add('aplayer-withlrc'); } - if (this.options.music.length > 1) { + if (this.options.audio.length > 1) { this.container.classList.add('aplayer-withlist'); } if (utils.isMobile) { @@ -53,8 +53,8 @@ class APlayer { if (this.options.lrc === 2 || this.options.lrc === true) { const lrcEle = this.container.getElementsByClassName('aplayer-lrc-content'); for (let i = 0; i < lrcEle.length; i++) { - if (this.options.music[i]) { - this.options.music[i].lrc = lrcEle[i].innerHTML; + if (this.options.audio[i]) { + this.options.audio[i].lrc = lrcEle[i].innerHTML; } } } @@ -73,7 +73,7 @@ class APlayer { this.lrc = new Lrc({ container: this.template.lrc, async: this.options.lrc === 3, - content: this.options.music.map((item) => item.lrc), + content: this.options.audio.map((item) => item.lrc), player: this, }); } @@ -139,16 +139,16 @@ class APlayer { this.template.author.innerHTML = ` - Error happens ╥﹏╥`; }); - // multiple music play + // multiple audio play this.on('ended', () => { if (this.options.loop === 'none') { if (this.options.order === 'list') { - if (this.playIndex < this.options.music.length - 1) { - this.switchAudio((this.playIndex + 1) % this.options.music.length); + if (this.playIndex < this.options.audio.length - 1) { + this.switchAudio((this.playIndex + 1) % this.options.audio.length); this.play(); } else { - this.switchAudio((this.playIndex + 1) % this.options.music.length); + this.switchAudio((this.playIndex + 1) % this.options.audio.length); this.pause(); } } @@ -169,7 +169,7 @@ class APlayer { } else if (this.options.loop === 'all') { if (this.options.order === 'list') { - this.switchAudio((this.playIndex + 1) % this.options.music.length); + this.switchAudio((this.playIndex + 1) % this.options.audio.length); } else if (this.options.order === 'random') { this.switchAudio(this.nextRandomNum()); @@ -181,9 +181,6 @@ class APlayer { this.volume(this.user.get('volume'), true, true); } - /** - * Set music - */ switchAudio (index) { this.handlePlayPromise(() => { if (typeof index !== 'undefined') { @@ -191,14 +188,14 @@ class APlayer { } // set html - if (this.options.music[this.playIndex].pic) { - this.template.pic.style.backgroundImage = `url('${this.options.music[this.playIndex].pic}')`; + if (this.options.audio[this.playIndex].cover) { + this.template.pic.style.backgroundImage = `url('${this.options.audio[this.playIndex].cover}')`; } else { this.template.pic.style.backgroundImage = ''; } - this.template.title.innerHTML = this.options.music[this.playIndex].title; - this.template.author.innerHTML = this.options.music[this.playIndex].author ? ' - ' + this.options.music[this.playIndex].author : ''; + this.template.title.innerHTML = this.options.audio[this.playIndex].name; + this.template.author.innerHTML = this.options.audio[this.playIndex].artist ? ' - ' + this.options.audio[this.playIndex].artist : ''; const light = this.container.getElementsByClassName('aplayer-list-light')[0]; if (light) { light.classList.remove('aplayer-list-light'); @@ -207,7 +204,7 @@ class APlayer { this.template.list.scrollTop = this.playIndex * 33; - this.audio.src = this.options.music[this.playIndex].url; + this.audio.src = this.options.audio[this.playIndex].url; this.seek(0); let playPromise; @@ -246,9 +243,6 @@ class APlayer { this.template.ptime.innerHTML = utils.secondToTime(time); } - /** - * Play music - */ play () { this.handlePlayPromise(() => { if (this.paused) { @@ -281,9 +275,6 @@ class APlayer { }); } - /** - * Pause music - */ pause () { this.handlePlayPromise(() => { if (!this.paused) { @@ -360,10 +351,10 @@ class APlayer { } /** - * get whether multiple music definitions are loaded + * get whether multiple audio definitions are loaded */ isMultiple () { - return this.options.music.length > 1; + return this.options.audio.length > 1; } /** @@ -385,23 +376,23 @@ class APlayer { } /** - * add music dynamically + * add audio dynamically * * @param {Array} newMusic */ addAudio (newMusic) { const wasSingle = !this.isMultiple(); - this.options.music = this.options.music.concat(newMusic); + this.options.audio = this.options.audio.concat(newMusic); let newItemHTML = ``; for (let i = 0; i < newMusic.length; i++) { newItemHTML += `