12 Commits
Author SHA1 Message Date
DIYgod 6c343db1dd docs: fix hls demo 2018-03-14 02:32:05 +08:00
DIYgod 4600381784 release v1.9.0 2018-03-14 02:28:31 +08:00
DIYgod 97165f07cc support hls; new option: audio.type, customAudioType 2018-03-14 02:27:08 +08:00
DIYgod 9864458475 optimize error handling; remove line before current time 2018-03-14 00:59:28 +08:00
DIYgod 454019fea0 fix asynchronization conflict 2018-03-14 00:25:54 +08:00
DIYgod 10d49c275a fix inverse disableTimeupdate 2018-03-14 00:19:24 +08:00
DIYgod cf44440907 new api: notice 2018-03-14 00:02:58 +08:00
DIYgod 1dc058c0c7 new api: setMode 2018-03-13 17:37:48 +08:00
DIYgod 738613fffe option: lrc -> lrcType 2018-03-13 17:13:15 +08:00
DIYgod c3f74ddfe8 docs: self-adapting theme according to cover 2018-03-13 17:01:31 +08:00
DIYgod ba7692984a new api: theme 2018-03-13 17:01:18 +08:00
DIYgod e10528397f replace dirty checking with timeupdate 2018-03-13 11:26:29 +08:00
20 changed files with 585 additions and 123 deletions
+61 -9
View File
@@ -2,7 +2,7 @@ const ap1 = new APlayer({
element: document.getElementById('player1'),
mini: false,
autoplay: false,
lrc: false,
lrcType: false,
mutex: true,
preload: 'metadata',
audio: [{
@@ -39,7 +39,7 @@ const ap2 = new APlayer({
element: document.getElementById('player2'),
mini: true,
autoplay: false,
lrc: false,
lrcType: false,
mutex: true,
audio: [{
name: '光るなら',
@@ -55,7 +55,7 @@ const ap3 = new APlayer({
element: document.getElementById('player3'),
mini: false,
autoplay: false,
lrc: 3,
lrcType: 3,
mutex: true,
audio: [{
name: '光るなら',
@@ -71,7 +71,7 @@ const ap4 = new APlayer({
element: document.getElementById('player4'),
mini: false,
autoplay: false,
lrc: false,
lrcType: false,
mutex: true,
theme: '#ad7a86',
order: 'random',
@@ -96,6 +96,13 @@ const ap4 = new APlayer({
cover: 'https://moeplayer.b0.upaiyun.com/aplayer/yourname.jpg',
lrc: 'https://moeplayer.b0.upaiyun.com/aplayer/yourname.lrc',
theme: '#505d6b'
}, {
name: '光るなら(HLS)',
artist: 'Goose house',
url: 'https://moeplayer.b0.upaiyun.com/aplayer/hls/hikarunara.m3u8',
cover: 'https://moeplayer.b0.upaiyun.com/aplayer/hikarunara.jpg',
theme: '#ebd0c2',
type: 'hls'
}]
});
@@ -103,9 +110,9 @@ const ap5 = new APlayer({
element: document.getElementById('player5'),
mini: false,
autoplay: false,
lrc: 3,
lrcType: 3,
mutex: true,
theme: '#ad7a86',
theme: '#e9e9e9',
listFolded: false,
listMaxHeight: '80px',
audio: [{
@@ -114,20 +121,65 @@ const ap5 = new APlayer({
url: 'https://moeplayer.b0.upaiyun.com/aplayer/hikarunara.mp3',
cover: 'https://moeplayer.b0.upaiyun.com/aplayer/hikarunara.jpg',
lrc: 'https://moeplayer.b0.upaiyun.com/aplayer/hikarunara.lrc',
theme: '#ebd0c2'
}, {
name: 'トリカゴ',
artist: 'XX:me',
url: 'https://moeplayer.b0.upaiyun.com/aplayer/darling.mp3',
cover: 'https://moeplayer.b0.upaiyun.com/aplayer/darling.jpg',
lrc: 'https://moeplayer.b0.upaiyun.com/aplayer/darling.lrc',
theme: '#46718b'
}, {
name: '前前前世',
artist: 'RADWIMPS',
url: 'https://moeplayer.b0.upaiyun.com/aplayer/yourname.mp3',
cover: 'https://moeplayer.b0.upaiyun.com/aplayer/yourname.jpg',
lrc: 'https://moeplayer.b0.upaiyun.com/aplayer/yourname.lrc',
theme: '#505d6b'
}]
});
const colorThief = new ColorThief();
ap5.on('switchaudio', (index) => {
if (!ap5.options.audio[index].theme) {
colorThief.getColorAsync(ap5.options.audio[index].cover, function (color) {
ap5.theme(`rgb(${color[0]}, ${color[1]}, ${color[2]})`, index);
});
}
});
const ap6 = new APlayer({
element: document.getElementById('player6'),
mutex: true,
audio: [{
name: '光るなら(HLS)',
artist: 'Goose house',
url: 'https://moeplayer.b0.upaiyun.com/aplayer/hls/hikarunara.m3u8',
cover: 'https://moeplayer.b0.upaiyun.com/aplayer/hikarunara.jpg',
theme: '#ebd0c2',
type: 'hls'
}]
});
const ap7 = new APlayer({
element: document.getElementById('player7'),
mutex: true,
audio: [{
name: '光るなら(HLS)',
artist: 'Goose house',
url: 'https://moeplayer.b0.upaiyun.com/aplayer/hls/hikarunara.m3u8',
cover: 'https://moeplayer.b0.upaiyun.com/aplayer/hikarunara.jpg',
theme: '#ebd0c2',
type: 'customHls',
}],
customAudioType: {
'customHls': function (audioElement, audio, player) {
if (Hls.isSupported()) {
const hls = new Hls();
hls.loadSource(audio.url);
hls.attachMedia(audioElement);
}
else if (audioElement.canPlayType('application/x-mpegURL') || audioElement.canPlayType('application/vnd.apple.mpegURL')) {
audioElement.src = audio.url;
}
else {
player.notice('Error: HLS is not supported.');
}
}
}
});
+6 -1
View File
@@ -43,7 +43,9 @@
font-size: 18px;
}
</style>
<script src="APlayer.js"></script>
<script src="https://cdn.jsdelivr.net/npm/hls.js/dist/hls.min.js"></script>
<script src="APlayer.js"></script>
<script src="https://cdn.jsdelivr.net/npm/color-thief-don@2.0.2/src/color-thief.js"></script>
</head>
<body>
<div class="container">
@@ -90,6 +92,9 @@
<div id="player5" class="aplayer"></div>
<h3>Narrow</h3>
<div id="player2" class="aplayer"></div>
<h3>HLS</h3>
<div id="player6" class="aplayer"></div>
<div id="player7" class="aplayer"></div>
</div>
<script src="demo.js"></script>
</body>
+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
File diff suppressed because one or more lines are too long
+129 -8
View File
@@ -81,8 +81,10 @@ audio.url | - | audio url
audio.cover | - | audio cover
audio.lrc | - | [see more details](https://aplayer.js.org/#/home?id=lrc)
audio.theme | - | main color when switching to this audio, it has priority over the above theme
audio.type | 'auto' | values: 'auto', 'hls', 'normal' or other custom type, [see more details](https://aplayer.js.org/#/home?id=mse-support)
customAudioType | - | [see more details](https://aplayer.js.org/#/home?id=mse-support)
mutex | true | prevent to play multiple player at the same time, pause other players when this player start play
lrc | false | [see more details](https://aplayer.js.org/#/home?id=lrc)
lrcType | 0 | [see more details](https://aplayer.js.org/#/home?id=lrc)
listFolded | false | indicate whether list should folded at first
listMaxHeight | - | list max height
@@ -105,7 +107,7 @@ const ap = new APlayer({
mutex: true,
listFolded: false,
listMaxHeight: '90px',
lrc: 3,
lrcType: 3,
audio: [
{
name: 'name1',
@@ -135,7 +137,7 @@ const ap = new APlayer({
+ `ap.pause()`: pause audio
+ `ap.seek(time: number)`: seek to specified time
+ `ap.seek(time: number)`: seek to specified time, the unit of time is second
```js
ap.seek(100);
@@ -178,6 +180,22 @@ const ap = new APlayer({
ap.volume(0.1, true);
```
+ `ap.theme(color: string, index: number)`: set player theme, the default of index is current audio index
```js
ap.theme('#000', 0);
```
+ `ap.setMode(mode: string)`: set player mode, the value of mode should be 'mini' or 'normal'
+ `ap.mode`: return current player mode, 'mini' or 'normal'
+ `ap.notice(text: string, time: number, opacity: number)`: show message, the unit of time is millisecond, the default of time is 2000, the default of opacity is 0.8, setting time to 0 can disable notice autohide.
```js
ap.notice('Amazing player', 2000, 0.8);
```
+ `ap.destroy()`: destroy player
+ `ap.audio`: native audio
@@ -232,10 +250,12 @@ Player events
- addaudio
- removeaudio
- destroy
- notice_show
- notice_hide
## LRC
We have three ways to pass LRC to APlayer, indicate the way to pass LRC by option `lrc`, then put lrc to option `audio.lrc` or HTML.
We have three ways to pass LRC to APlayer, indicate the way to pass LRC by option `lrcType`, then put lrc to option `audio.lrc` or HTML.
<div class="aplayer-wrap">
<div id="aplayer4"><button class="docute-button load">Click to load player</div>
@@ -248,7 +268,7 @@ The first way, put LRC to a LRC file, LRC file will be loaded when this audio st
```js
const ap = new APlayer({
container: document.getElementById('aplayer'),
lrc: 3,
lrcType: 3,
audio: {
name: 'name',
artist: 'artist',
@@ -266,7 +286,7 @@ The second way, put LRC to a JS string.
```js
const ap = new APlayer({
container: document.getElementById('aplayer'),
lrc: 1,
lrcType: 1,
audio: {
name: 'name',
artist: 'artist',
@@ -303,7 +323,7 @@ The third way, put LRC to HTML.
```js
const ap = new APlayer({
container: document.getElementById('aplayer'),
lrc: 2,
lrcType: 2,
audio: [[
{
name: 'name1',
@@ -350,7 +370,7 @@ const ap = new APlayer({
container: document.getElementById('player'),
listFolded: false,
listMaxHeight: '90px',
lrc: 3,
lrcType: 3,
audio: [
{
name: 'name1',
@@ -393,6 +413,107 @@ const ap = new APlayer({
});
```
## MSE support
### HLS
It requires the library [hls.js](https://github.com/video-dev/hls.js) and it should be loaded before `APlayer.min.js`.
<div class="aplayer-wrap">
<div id="aplayer7"><button class="docute-button load">Click to load player</div>
</div>
```html
<link rel="stylesheet" href="APlayer.min.css">
<div id="aplayer"></div>
<script src="hls.min.js"></script>
<script src="APlayer.min.js"></script>
```
```js
const ap = new APlayer({
container: document.getElementById('aplayer'),
audio: [{
name: 'HLS',
artist: 'artist',
url: 'url.m3u8',
cover: 'cover.jpg',
type: 'hls'
}]
});
```
```js
// another way, use customType
const ap = new APlayer({
container: document.getElementById('aplayer'),
audio: [{
name: 'HLS',
artist: 'artist',
url: 'url.m3u8',
cover: 'cover.jpg',
type: 'customHls'
}],
customAudioType: {
'customHls': function (audioElement, audio, player) {
if (Hls.isSupported()) {
const hls = new Hls();
hls.loadSource(audio.url);
hls.attachMedia(audioElement);
}
else if (audioElement.canPlayType('application/x-mpegURL') || audioElement.canPlayType('application/vnd.apple.mpegURL')) {
audioElement.src = audio.url;
}
else {
player.notice('Error: HLS is not supported.');
}
}
}
});
```
## Self-adapting theme according to cover
It requires the library [color-thief](https://github.com/lokesh/color-thief/blob/master/src/color-thief.js).
<div class="aplayer-wrap">
<div id="aplayer8"><button class="docute-button load">Click to load player</div>
</div>
```html
<link rel="stylesheet" href="APlayer.min.css">
<div id="aplayer"></div>
<script src="APlayer.min.js"></script>
<script src="color-thief.js"></script>
```
```js
const ap = new APlayer({
container: document.getElementById('aplayer'),
theme: '#e9e9e9',
audio: [{
name: 'name1',
artist: 'artist1',
url: 'url1.mp3',
cover: 'cover1.jpg'
}, {
name: 'name2',
artist: 'artist2',
url: 'url2.mp3',
cover: 'cover2.jpg'
}]
});
const colorThief = new ColorThief();
ap.on('switchaudio', (index) => {
if (!ap.options.audio[index].theme) {
colorThief.getColorAsync(ap.options.audio[index].cover, (color) => {
ap.theme(`rgb(${color[0]}, ${color[1]}, ${color[2]})`, index);
});
}
});
```
## CDN
- [jsDelivr](https://www.jsdelivr.com/package/npm/aplayer)
+68 -4
View File
@@ -82,7 +82,7 @@ function aplayer1 () {
window.ap1 = new APlayer({
container: document.getElementById('aplayer1'),
theme: '#F57F17',
lrc: 3,
lrcType: 3,
audio: [{
name: '光るなら',
artist: 'Goose house',
@@ -133,7 +133,7 @@ function aplayer3 () {
mutex: true,
listFolded: false,
listMaxHeight: '90px',
lrc: 3,
lrcType: 3,
audio: [{
name: '光るなら',
artist: 'Goose house',
@@ -162,7 +162,7 @@ function aplayer3 () {
function aplayer4 () {
window.ap4 = new APlayer({
container: document.getElementById('aplayer4'),
lrc: 3,
lrcType: 3,
audio: [{
name: '光るなら',
artist: 'Goose house',
@@ -177,7 +177,7 @@ function aplayer4 () {
function aplayer5 () {
window.ap5 = new APlayer({
container: document.getElementById('aplayer5'),
lrc: 3,
lrcType: 3,
audio: [{
name: '光るなら',
artist: 'Goose house',
@@ -215,4 +215,68 @@ function aplayer6 () {
theme: '#ebd0c2'
}]
});
}
function aplayer7 () {
window.ap7 = new APlayer({
container: document.getElementById('aplayer7'),
audio: [{
name: '光るなら(HLS)',
artist: 'Goose house',
url: 'https://moeplayer.b0.upaiyun.com/aplayer/hls/hikarunara.m3u8',
cover: 'https://moeplayer.b0.upaiyun.com/aplayer/hikarunara.jpg',
theme: '#ebd0c2',
type: 'hls'
}, {
name: '光るなら',
artist: 'Goose house',
url: 'https://moeplayer.b0.upaiyun.com/aplayer/hikarunara.mp3',
cover: 'https://moeplayer.b0.upaiyun.com/aplayer/hikarunara.jpg',
theme: '#ebd0c2'
}, {
name: 'トリカゴ',
artist: 'XX:me',
url: 'https://moeplayer.b0.upaiyun.com/aplayer/darling.mp3',
cover: 'https://moeplayer.b0.upaiyun.com/aplayer/darling.jpg',
theme: '#46718b'
}, {
name: '前前前世',
artist: 'RADWIMPS',
url: 'https://moeplayer.b0.upaiyun.com/aplayer/yourname.mp3',
cover: 'https://moeplayer.b0.upaiyun.com/aplayer/yourname.jpg',
theme: '#505d6b'
}]
});
}
function aplayer8 () {
window.ap8 = new APlayer({
container: document.getElementById('aplayer8'),
theme: '#e9e9e9',
audio: [{
name: '光るなら',
artist: 'Goose house',
url: 'https://moeplayer.b0.upaiyun.com/aplayer/hikarunara.mp3',
cover: 'https://moeplayer.b0.upaiyun.com/aplayer/hikarunara.jpg',
}, {
name: 'トリカゴ',
artist: 'XX:me',
url: 'https://moeplayer.b0.upaiyun.com/aplayer/darling.mp3',
cover: 'https://moeplayer.b0.upaiyun.com/aplayer/darling.jpg',
}, {
name: '前前前世',
artist: 'RADWIMPS',
url: 'https://moeplayer.b0.upaiyun.com/aplayer/yourname.mp3',
cover: 'https://moeplayer.b0.upaiyun.com/aplayer/yourname.jpg',
}]
});
const colorThief = new ColorThief();
window.ap8.on('switchaudio', function (index) {
if (!window.ap8.options.audio[index].theme) {
colorThief.getColorAsync(window.ap8.options.audio[index].cover, function (color) {
window.ap8.theme(`rgb(${color[0]}, ${color[1]}, ${color[2]})`, index);
});
}
});
}
+4 -2
View File
@@ -16,8 +16,10 @@
gtag('config', 'UA-48084758-8');
</script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer@1.8.0/dist/APlayer.min.css">
<script src="https://cdn.jsdelivr.net/npm/aplayer@1.8.0/dist/APlayer.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer@1.9.0/dist/APlayer.min.css">
<script src="https://cdn.jsdelivr.net/npm/hls.js/dist/hls.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/aplayer@1.9.0/dist/APlayer.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/color-thief-don@2.0.2/src/color-thief.js"></script>
<style>
body {
text-rendering: auto;
+129 -8
View File
@@ -82,8 +82,10 @@ audio.url | - | 音频链接
audio.cover | - | 音频封面
audio.lrc | - | [详情](https://aplayer.js.org/#/home?id=lrc)
audio.theme | - | 切换到此音频时的主题色,比上面的 theme 优先级高
audio.type | 'auto' | 可选值: 'auto', 'hls', 'normal' 或其他自定义类型, [详情](https://aplayer.js.org/#/home?id=mse-support)
customAudioType | - | 自定义类型,[详情](https://aplayer.js.org/#/home?id=mse-support)
mutex | true | 互斥,阻止多个播放器同时播放,当前播放器播放时暂停其他播放器
lrc | false | [详情](https://aplayer.js.org/#/home?id=lrc)
lrcType | 0 | [详情](https://aplayer.js.org/#/home?id=lrc)
listFolded | false | 列表默认折叠
listMaxHeight | - | 列表最大高度
@@ -106,7 +108,7 @@ const ap = new APlayer({
mutex: true,
listFolded: false,
listMaxHeight: '90px',
lrc: 3,
lrcType: 3,
audio: [
{
name: 'name1',
@@ -136,7 +138,7 @@ const ap = new APlayer({
+ `ap.pause()`: 暂停音频
+ `ap.seek(time: number)`: 跳转到特定时间
+ `ap.seek(time: number)`: 跳转到特定时间,时间的单位为秒
```js
ap.seek(100);
@@ -179,6 +181,22 @@ const ap = new APlayer({
ap.volume(0.1, true);
```
+ `ap.theme(color: string, index: number)`: 设置播放器主题色, index 默认为当前音频的 index
```js
ap.theme('#000', 0);
```
+ `ap.setMode(mode: string)`: 设置播放器模式,mode 取值应为 'mini' 或 'normal'
+ `ap.mode`: 返回播放器当前模式,'mini' 或 'normal'
+ `ap.notice(text: string, time: number, opacity: number)`: 显示通知,时间的单位为毫秒,默认时间 2000 毫秒,默认透明度 0.8,设置时间为 0 可以取消通知自动隐藏
```js
ap.notice('Amazing player', 2000, 0.8);
```
+ `ap.destroy()`: 销毁播放器
+ `ap.audio`: 原生 video
@@ -233,10 +251,12 @@ ap.on('ended', function () {
- addaudio
- removeaudio
- destroy
- notice_show
- notice_hide
## 歌词
我们有三种方式来给 APlayer 传递歌词,使用 `lrc` 参数指明使用哪种方式,然后把歌词放到 `audio.lrc` 参数或者 HTML 里。
我们有三种方式来给 APlayer 传递歌词,使用 `lrcType` 参数指明使用哪种方式,然后把歌词放到 `audio.lrc` 参数或者 HTML 里。
<div class="aplayer-wrap">
<div id="aplayer4"><button class="docute-button load">点击加载播放器</div>
@@ -249,7 +269,7 @@ ap.on('ended', function () {
```js
const ap = new APlayer({
container: document.getElementById('aplayer'),
lrc: 3,
lrcType: 3,
audio: {
name: 'name',
artist: 'artist',
@@ -267,7 +287,7 @@ const ap = new APlayer({
```js
const ap = new APlayer({
container: document.getElementById('aplayer'),
lrc: 1,
lrcType: 1,
audio: {
name: 'name',
artist: 'artist',
@@ -304,7 +324,7 @@ const ap = new APlayer({
```js
const ap = new APlayer({
container: document.getElementById('aplayer'),
lrc: 2,
lrcType: 2,
audio: [[
{
name: 'name1',
@@ -351,7 +371,7 @@ const ap = new APlayer({
container: document.getElementById('player'),
listFolded: false,
listMaxHeight: '90px',
lrc: 3,
lrcType: 3,
audio: [
{
name: 'name1',
@@ -394,6 +414,107 @@ const ap = new APlayer({
});
```
## MSE 支持
### HLS
需要在 `APlayer.min.js` 前面加载 [hls.js](https://github.com/video-dev/hls.js)。
<div class="aplayer-wrap">
<div id="aplayer7"><button class="docute-button load">点击加载播放器</div>
</div>
```html
<link rel="stylesheet" href="APlayer.min.css">
<div id="aplayer"></div>
<script src="hls.min.js"></script>
<script src="APlayer.min.js"></script>
```
```js
const ap = new APlayer({
container: document.getElementById('aplayer'),
audio: [{
name: 'HLS',
artist: 'artist',
url: 'url.m3u8',
cover: 'cover.jpg',
type: 'hls'
}]
});
```
```js
// 另一种方式,使用 customAudioType
const ap = new APlayer({
container: document.getElementById('aplayer'),
audio: [{
name: 'HLS',
artist: 'artist',
url: 'url.m3u8',
cover: 'cover.jpg',
type: 'customHls'
}],
customAudioType: {
'customHls': function (audioElement, audio, player) {
if (Hls.isSupported()) {
const hls = new Hls();
hls.loadSource(audio.url);
hls.attachMedia(audioElement);
}
else if (audioElement.canPlayType('application/x-mpegURL') || audioElement.canPlayType('application/vnd.apple.mpegURL')) {
audioElement.src = audio.url;
}
else {
player.notice('Error: HLS is not supported.');
}
}
}
});
```
## 根据封面自适应主题色
需要额外加载 [color-thief.js](https://github.com/lokesh/color-thief/blob/master/src/color-thief.js)
<div class="aplayer-wrap">
<div id="aplayer8"><button class="docute-button load">点击加载播放器</div>
</div>
```html
<link rel="stylesheet" href="APlayer.min.css">
<div id="aplayer"></div>
<script src="APlayer.min.js"></script>
<script src="color-thief.js"></script>
```
```js
const ap = new APlayer({
container: document.getElementById('aplayer'),
theme: '#e9e9e9',
audio: [{
name: 'name1',
artist: 'artist1',
url: 'url1.mp3',
cover: 'cover1.jpg'
}, {
name: 'name2',
artist: 'artist2',
url: 'url2.mp3',
cover: 'cover2.jpg'
}]
});
const colorThief = new ColorThief();
ap.on('switchaudio', (index) => {
if (!ap.options.audio[index].theme) {
colorThief.getColorAsync(ap.options.audio[index].cover, (color) => {
ap.theme(`rgb(${color[0]}, ${color[1]}, ${color[2]})`, index);
});
}
});
```
## CDN
- [jsDelivr](https://www.jsdelivr.com/package/npm/aplayer)
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "aplayer",
"version": "1.8.0",
"version": "1.9.0",
"description": "Wow, such a beautiful html5 music player",
"main": "dist/APlayer.min.js",
"style": "dist/APlayer.min.css",
+24 -1
View File
@@ -11,6 +11,7 @@ $aplayer-height-lrc: $aplayer-height + $lrc-height - 6;
overflow: hidden;
user-select: none;
line-height: initial;
position: relative;
* {
box-sizing: content-box;
@@ -62,7 +63,8 @@ $aplayer-height-lrc: $aplayer-height + $lrc-height - 6;
.aplayer-list {
display: none;
}
.aplayer-pic {
.aplayer-pic,
.aplayer-body {
height: $aplayer-height;
width: $aplayer-height;
}
@@ -91,6 +93,10 @@ $aplayer-height-lrc: $aplayer-height + $lrc-height - 6;
}
}
.aplayer-body {
position: relative;
}
.aplayer-icon {
width: 15px;
height: 15px;
@@ -510,6 +516,23 @@ $aplayer-height-lrc: $aplayer-height + $lrc-height - 6;
}
}
}
.aplayer-notice {
opacity: 0;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 12px;
border-radius: 4px;
padding: 5px 10px;
transition: all .3s ease-in-out;
overflow: hidden;
color: #fff;
pointer-events: none;
background-color: #f4f4f5;
color: #909399;
}
}
@keyframes aplayer-roll {
+2 -2
View File
@@ -62,11 +62,11 @@ class Controller {
percentage = Math.min(percentage, 1);
this.player.bar.set('played', percentage, 'width');
this.player.seek(this.player.bar.get('played', 'width') * this.player.audio.duration);
this.player.timer.enable('progress');
this.player.disableTimeupdate = false;
};
this.player.template.barWrap.addEventListener(utils.nameMap.dragStart, () => {
this.player.timer.disable('progress');
this.player.disableTimeupdate = true;
document.addEventListener(utils.nameMap.dragMove, thumbMove);
document.addEventListener(utils.nameMap.dragEnd, thumbUp);
});
+1 -1
View File
@@ -9,7 +9,7 @@ class Events {
'timeupdate', 'volumechange', 'waiting'
];
this.playerEvents = [
'destroy', 'switchaudio', 'addaudio', 'removeaudio'
'destroy', 'switchaudio', 'addaudio', 'removeaudio', 'notice_show', 'notice_hide'
];
}
+2 -2
View File
@@ -39,12 +39,12 @@ class Lrc {
this.parsed[index] = [['00:00', 'Loading']];
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
if (index === this.player.playIndex && xhr.readyState === 4) {
if (xhr.status >= 200 && xhr.status < 300 || xhr.status === 304) {
this.parsed[index] = this.parse(xhr.responseText);
}
else {
console.log('Request was unsuccessful: ' + xhr.status);
this.player.notice(`LRC file request fails: status ${xhr.status}`);
this.parsed[index] = [['00:00', 'Not available']];
}
+2 -1
View File
@@ -6,7 +6,7 @@ export default (options) => {
mini: options.narrow || false,
autoplay: false,
mutex: true,
lrc: options.showlrc || 0,
lrcType: options.showlrc || options.lrc || 0,
preload: 'auto',
theme: '#b7daff',
loop: 'all',
@@ -30,6 +30,7 @@ export default (options) => {
item.name = item.name || item.title;
item.artist = item.artist || item.author;
item.cover = item.cover || item.pic;
item.type = item.type || 'normal';
return item;
});
+103 -21
View File
@@ -27,11 +27,12 @@ class APlayer {
this.playIndex = 0;
this.paused = true;
this.playedPromise = Promise.resolve();
this.mode = 'normal';
this.randomOrder = utils.randomOrder(this.options.audio.length);
this.container.classList.add('aplayer');
if (this.options.lrc) {
if (this.options.lrcType) {
this.container.classList.add('aplayer-withlrc');
}
if (this.options.audio.length > 1) {
@@ -45,12 +46,12 @@ class APlayer {
this.container.classList.add('aplayer-arrow');
}
if (this.options.mini) {
this.container.classList.add('aplayer-narrow');
this.setMode('mini');
}
// save lrc
this.container = this.options.container;
if (this.options.lrc === 2 || this.options.lrc === true) {
if (this.options.lrcType === 2 || this.options.lrcType === true) {
const lrcEle = this.container.getElementsByClassName('aplayer-lrc-content');
for (let i = 0; i < lrcEle.length; i++) {
if (this.options.audio[i]) {
@@ -69,10 +70,10 @@ class APlayer {
this.template.time.classList.add('aplayer-time-narrow');
}
if (this.options.lrc) {
if (this.options.lrcType) {
this.lrc = new Lrc({
container: this.template.lrc,
async: this.options.lrc === 3,
async: this.options.lrcType === 3,
content: this.options.audio.map((item) => item.lrc),
player: this,
});
@@ -104,8 +105,8 @@ class APlayer {
this.audio.preload = this.options.preload;
for (let i = 0; i < this.events.audioEvents.length; i++) {
this.audio.addEventListener(this.events.audioEvents[i], () => {
this.events.trigger(this.events.audioEvents[i]);
this.audio.addEventListener(this.events.audioEvents[i], (e) => {
this.events.trigger(this.events.audioEvents[i], e);
});
}
@@ -121,6 +122,17 @@ class APlayer {
}
});
this.on('timeupdate', () => {
if (!this.disableTimeupdate) {
this.bar.set('played', this.audio.currentTime / this.audio.duration, 'width');
this.lrc && this.lrc.update();
const currentTime = utils.secondToTime(this.audio.currentTime);
if (this.template.ptime.innerHTML !== currentTime) {
this.template.ptime.innerHTML = currentTime;
}
}
});
// show audio time: the metadata has loaded or changed
this.on('durationchange', () => {
if (this.audio.duration !== 1) { // compatibility: Android browsers will output 1 at first
@@ -136,7 +148,7 @@ class APlayer {
// audio download error: an error occurs
this.on('error', () => {
this.template.author.innerHTML = ` - Error happens ╥﹏╥`;
this.notice('An audio error has occurred.');
});
// multiple audio play
@@ -188,21 +200,17 @@ class APlayer {
this.playIndex = index;
}
const audio = this.options.audio[this.playIndex];
// set html
if (this.options.audio[this.playIndex].cover) {
this.template.pic.style.backgroundImage = `url('${this.options.audio[this.playIndex].cover}')`;
if (audio.cover) {
this.template.pic.style.backgroundImage = `url('${audio.cover}')`;
}
else {
this.template.pic.style.backgroundImage = '';
}
if (this.options.audio[this.playIndex].theme) {
this.template.pic.style.backgroundColor = this.options.audio[this.playIndex].theme;
this.template.played.style.background = this.options.audio[this.playIndex].theme;
this.template.thumb.style.background = this.options.audio[this.playIndex].theme;
this.template.volume.style.background = this.options.audio[this.playIndex].theme;
}
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 : '';
this.theme();
this.template.title.innerHTML = audio.name;
this.template.author.innerHTML = audio.artist ? ' - ' + audio.artist : '';
const light = this.container.getElementsByClassName('aplayer-list-light')[0];
if (light) {
light.classList.remove('aplayer-list-light');
@@ -211,7 +219,47 @@ class APlayer {
this.template.list.scrollTop = this.playIndex * 33;
this.audio.src = this.options.audio[this.playIndex].url;
// mse
if (this.hls) {
this.hls.destroy();
this.hls = null;
}
let type = audio.type;
if (this.options.customAudioType && this.options.customAudioType[type]) {
if (Object.prototype.toString.call(this.options.customAudioType[type]) === '[object Function]') {
this.options.customAudioType[type](this.audio, audio, this);
}
else {
console.error(`Illegal customType: ${type}`);
}
}
else {
if (!type || type === 'auto') {
if (/m3u8(#|\?|$)/i.exec(audio.url)) {
type = 'hls';
}
else {
type = 'normal';
}
}
if (type === 'hls') {
if (Hls.isSupported()) {
this.hls = new Hls();
this.hls.loadSource(audio.url);
this.hls.attachMedia(this.audio);
}
else if (this.audio.canPlayType('application/x-mpegURL') || this.audio.canPlayType('application/vnd.apple.mpegURL')) {
this.audio.src = audio.url;
}
else {
this.notice('Error: HLS is not supported.');
}
}
else if (type === 'normal') {
this.audio.src = audio.url;
}
}
this.seek(0);
let playPromise;
@@ -233,6 +281,17 @@ class APlayer {
});
}
theme (color = this.options.audio[this.playIndex].theme, index = this.playIndex) {
this.options.audio[index].theme = color;
this.template.listCurs[index].style.backgroundColor = color;
if (index === this.playIndex) {
this.template.pic.style.backgroundColor = color;
this.template.played.style.background = color;
this.template.thumb.style.background = color;
this.template.volume.style.background = color;
}
}
seek (time) {
time = Math.max(time, 0);
if (this.audio.duration) {
@@ -268,7 +327,6 @@ class APlayer {
});
this.timer.enable('loading');
this.timer.enable('progress');
if (this.options.mutex) {
for (let i = 0; i < instances.length; i++) {
@@ -298,7 +356,6 @@ class APlayer {
this.audio.pause();
this.timer.disable('loading');
this.timer.disable('progress');
});
}
@@ -462,6 +519,31 @@ class APlayer {
this.events.trigger('destroy');
}
setMode (mode = 'normal') {
this.mode = mode;
if (mode === 'mini') {
this.container.classList.add('aplayer-narrow');
}
else if (mode === 'normal') {
this.container.classList.remove('aplayer-narrow');
}
}
notice (text, time = 2000, opacity = 0.8) {
this.template.notice.innerHTML = text;
this.template.notice.style.opacity = opacity;
if (this.noticeTime) {
clearTimeout(this.noticeTime);
}
this.events.trigger('notice_show', text);
if (time) {
this.noticeTime = setTimeout(() => {
this.template.notice.style.opacity = 0;
this.events.trigger('notice_hide');
}, time);
}
}
handlePlayPromise (callback) {
this.playedPromise = this.playedPromise.then(callback).catch((err) => {
console.error(err);
+2
View File
@@ -32,6 +32,7 @@ class Template {
this.button = this.container.querySelector('.aplayer-button');
this.list = this.container.querySelector('.aplayer-list');
this.listOl = this.container.querySelector('.aplayer-list ol');
this.listCurs = this.container.querySelectorAll('.aplayer-list-cur');
this.played = this.container.querySelector('.aplayer-played');
this.loaded = this.container.querySelector('.aplayer-loaded');
this.thumb = this.container.querySelector('.aplayer-thumb');
@@ -46,6 +47,7 @@ class Template {
this.title = this.container.querySelector('.aplayer-title');
this.author = this.container.querySelector('.aplayer-author');
this.dtime = this.container.querySelector('.aplayer-dtime');
this.notice = this.container.querySelector('.aplayer-notice');
}
}
+1 -15
View File
@@ -1,4 +1,3 @@
import utils from './utils';
class Timer {
constructor (player) {
@@ -15,7 +14,7 @@ class Timer {
}
)();
this.types = ['loading', 'progress'];
this.types = ['loading'];
this.init();
}
@@ -27,19 +26,6 @@ class Timer {
});
}
initprogressChecker () {
this.progressChecker = setInterval(() => {
if (this.enableprogressChecker) {
this.player.bar.set('played', this.player.audio.currentTime / this.player.audio.duration, 'width');
this.player.lrc && this.player.lrc.update();
const currentTime = utils.secondToTime(this.player.audio.currentTime);
if (this.player.template.ptime.innerHTML !== currentTime) {
this.player.template.ptime.innerHTML = currentTime;
}
}
}, 100);
}
initloadingChecker () {
let lastPlayPos = 0;
let currentPlayPos = 0;
+46 -43
View File
@@ -1,58 +1,61 @@
<div class="aplayer-pic" style="{{ if cover }}background-image: url(&quot;{{ cover }}&quot;);{{ /if }}background-color: {{ options.theme }};">
<div class="aplayer-button aplayer-play">{{@ icons.play }}</div>
</div>
<div class="aplayer-info">
<div class="aplayer-music">
<span class="aplayer-title"></span>
<span class="aplayer-author"></span>
<div class="aplayer-body">
<div class="aplayer-pic" style="{{ if cover }}background-image: url(&quot;{{ cover }}&quot;);{{ /if }}background-color: {{ options.theme }};">
<div class="aplayer-button aplayer-play">{{@ icons.play }}</div>
</div>
<div class="aplayer-lrc">
<div class="aplayer-lrc-contents" style="transform: translateY(0); -webkit-transform: translateY(0);"></div>
</div>
<div class="aplayer-controller">
<div class="aplayer-bar-wrap">
<div class="aplayer-bar">
<div class="aplayer-loaded" style="width: 0"></div>
<div class="aplayer-played" style="width: 0; background: {{ options.theme }};">
<span class="aplayer-thumb" style="background: {{ options.theme }};">
<span class="aplayer-loading-icon">{{@ icons.loading }}</span>
</span>
</div>
</div>
<div class="aplayer-info">
<div class="aplayer-music">
<span class="aplayer-title"></span>
<span class="aplayer-author"></span>
</div>
<div class="aplayer-time">
<span class="aplayer-time-inner">
- <span class="aplayer-ptime">00:00</span> / <span class="aplayer-dtime">00:00</span>
</span>
<div class="aplayer-volume-wrap">
<button type="button" class="aplayer-icon aplayer-icon-volume-down">
{{@ icons.volumeDown }}
</button>
<div class="aplayer-volume-bar-wrap">
<div class="aplayer-volume-bar">
<div class="aplayer-volume" style="height: 80%; background: {{ options.theme }};"></div>
<div class="aplayer-lrc">
<div class="aplayer-lrc-contents" style="transform: translateY(0); -webkit-transform: translateY(0);"></div>
</div>
<div class="aplayer-controller">
<div class="aplayer-bar-wrap">
<div class="aplayer-bar">
<div class="aplayer-loaded" style="width: 0"></div>
<div class="aplayer-played" style="width: 0; background: {{ options.theme }};">
<span class="aplayer-thumb" style="background: {{ options.theme }};">
<span class="aplayer-loading-icon">{{@ icons.loading }}</span>
</span>
</div>
</div>
</div>
{{ if options.audio.length > 1 }}
<button type="button" class="aplayer-icon aplayer-icon-order">
{{ if options.order === 'list' }}{{@ icons.orderList }}{{ else if options.order === 'random' }}{{@ icons.orderRandom }}{{ /if }}
</button>
{{ /if }}
<button type="button" class="aplayer-icon aplayer-icon-loop">
{{ if options.loop === 'one' }}{{@ icons.loopOne }}{{ else if options.loop === 'all' }}{{@ icons.loopAll }}{{ else if options.loop === 'none' }}{{@ icons.loopNone }}{{ /if }}
</button>
<button type="button" class="aplayer-icon aplayer-icon-menu">
{{@ icons.menu }}
</button>
<div class="aplayer-time">
<span class="aplayer-time-inner">
<span class="aplayer-ptime">00:00</span> / <span class="aplayer-dtime">00:00</span>
</span>
<div class="aplayer-volume-wrap">
<button type="button" class="aplayer-icon aplayer-icon-volume-down">
{{@ icons.volumeDown }}
</button>
<div class="aplayer-volume-bar-wrap">
<div class="aplayer-volume-bar">
<div class="aplayer-volume" style="height: 80%; background: {{ options.theme }};"></div>
</div>
</div>
</div>
{{ if options.audio.length > 1 }}
<button type="button" class="aplayer-icon aplayer-icon-order">
{{ if options.order === 'list' }}{{@ icons.orderList }}{{ else if options.order === 'random' }}{{@ icons.orderRandom }}{{ /if }}
</button>
{{ /if }}
<button type="button" class="aplayer-icon aplayer-icon-loop">
{{ if options.loop === 'one' }}{{@ icons.loopOne }}{{ else if options.loop === 'all' }}{{@ icons.loopAll }}{{ else if options.loop === 'none' }}{{@ icons.loopNone }}{{ /if }}
</button>
<button type="button" class="aplayer-icon aplayer-icon-menu">
{{@ icons.menu }}
</button>
</div>
</div>
</div>
<div class="aplayer-notice"></div>
</div>
<div class="aplayer-list{{ if options.listFolded }} aplayer-list-hide{{ /if }}"{{ if options.listMaxHeight }} style="max-height: {{ options.listMaxHeight }}"{{ /if }}>
<ol{{ if options.listMaxHeight }} style="max-height: {{ options.listMaxHeight }}"{{ /if }}>
{{each options.audio}}
<li>
<span class="aplayer-list-cur" style="background: {{ $value.theme || options.theme }};"></span>
<span class="aplayer-list-cur" style="background-color: {{ $value.theme || options.theme }};"></span>
<span class="aplayer-list-index">{{ $index + 1 }}</span>
<span class="aplayer-list-title">{{ $value.name }}</span>
<span class="aplayer-list-author">{{ $value.artist }}</span>