new api: notice

This commit is contained in:
DIYgod 2018-03-14 00:02:58 +08:00
parent 1dc058c0c7
commit cf44440907
No known key found for this signature in database
GPG Key ID: EC0B76A252D3EF67
7 changed files with 105 additions and 47 deletions

View File

@ -135,7 +135,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);
@ -188,6 +188,12 @@ const ap = new APlayer({
+ `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
@ -242,6 +248,8 @@ Player events
- addaudio
- removeaudio
- destroy
- notice_show
- notice_hide
## LRC

View File

@ -136,7 +136,7 @@ const ap = new APlayer({
+ `ap.pause()`: 暂停音频
+ `ap.seek(time: number)`: 跳转到特定时间
+ `ap.seek(time: number)`: 跳转到特定时间,时间的单位为秒
```js
ap.seek(100);
@ -189,6 +189,12 @@ const ap = new APlayer({
+ `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
@ -243,6 +249,8 @@ ap.on('ended', function () {
- addaudio
- removeaudio
- destroy
- notice_show
- notice_hide
## 歌词

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 {

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'
];
}

View File

@ -148,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.', 0);
});
// multiple audio play
@ -488,6 +488,21 @@ class APlayer {
}
}
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);

View File

@ -47,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');
}
}

View File

@ -1,52 +1,55 @@
<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 }}>