APlayer
🍭 Wow, such a beautiful HTML5 music player
Special Sponsors
Installation
Using npm:
1 | npm install aplayer --save |
Using Yarn:
1 | yarn add aplayer |
Quick Start
1 | <link rel="stylesheet" href="APlayer.min.css"> |
1 | const ap = new APlayer({ |
Work with module bundler:
1 | import 'aplayer/dist/APlayer.min.css'; |
Options
| Name | Default | Description |
|---|---|---|
| container | document.querySelector(‘.aplayer’) | player container |
| fixed | false | enable fixed mode, see more details |
| mini | false | enable mini mode, see more details |
| autoplay | false | audio autoplay |
| theme | ‘#b7daff’ | main color |
| loop | ‘all’ | player loop play, values: ‘all’, ‘one’, ‘none’ |
| order | ‘list’ | player play order, values: ‘list’, ‘random’ |
| preload | ‘auto’ | values: ‘none’, ‘metadata’, ‘auto’ |
| volume | 0.7 | default volume, notice that player will remember user setting, default volume will not work after user set volume themselves |
| audio | - | audio info, should be an object or object array |
| audio.name | - | audio name |
| audio.artist | - | audio artist |
| audio.url | - | audio url |
| audio.cover | - | audio cover |
| audio.lrc | - | see more details |
| 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 |
| customAudioType | - | see more details |
| mutex | true | prevent to play multiple player at the same time, pause other players when this player start play |
| lrcType | 0 | see more details |
| listFolded | false | indicate whether list should folded at first |
| listMaxHeight | - | list max height |
| storageName | ‘aplayer-setting’ | localStorage key that store player setting |
For example:
1 | const ap = new APlayer({ |
API
-
APlayer.version: static property, return the version of APlayer -
ap.play(): play audio -
ap.pause(): pause audio -
ap.seek(time: number): seek to specified time, the unit of time is second1
ap.seek(100);
-
ap.toggle(): toggle between play and pause -
ap.on(event: string, handler: function): bind audio and player events, see more details -
ap.volume(percentage: number, nostorage: boolean): set audio volume1
ap.volume(0.1, true);
-
ap.theme(color: string, index: number): set player theme, the default of index is current audio index1
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.1
ap.notice('Amazing player', 2000, 0.8);
-
ap.skipBack(): skip to previous audio -
ap.skipForward(): skip to next audio -
ap.destroy(): destroy player -
ap.lrc-
ap.lrc.show(): show lrc -
ap.lrc.hide(): hide lrc -
ap.lrc.toggle(): toggle lrc between show and hide
-
-
ap.list-
ap.list.show(): show list -
ap.list.hide(): hide list -
ap.list.toggle(): toggle list between show and hide -
ap.list.add(audios: array | object): add new audios to the list
1
2
3
4
5
6
7
8ap.list.add([{
name: 'name',
artist: 'artist',
url: 'url.mp3',
cover: 'cover.jpg',
lrc: 'lrc.lrc',
theme: '#ebd0c2'
}]);-
ap.list.remove(index: number): remove an audio from the list
1
ap.list.remove(1);
-
ap.list.switch(): switch to an audio in the list
1
ap.list.switch(1);
-
ap.list.clear(): remove all audios from the list
-
-
ap.audio: native audio -
ap.audio.currentTime: returns the current playback position -
ap.audio.duration: returns audio total time -
ap.audio.paused: returns whether the audio paused -
most native api are supported
Event binding
ap.on(event, handler)
1 | ap.on('ended', function () { |
Audio events
-
abort
-
canplay
-
canplaythrough
-
durationchange
-
emptied
-
ended
-
error
-
loadeddata
-
loadedmetadata
-
loadstart
-
mozaudioavailable
-
pause
-
play
-
playing
-
progress
-
ratechange
-
seeked
-
seeking
-
stalled
-
suspend
-
timeupdate
-
volumechange
-
waiting
Player events
-
listshow
-
listhide
-
listadd
-
listremove
-
listswitch
-
listclear
-
noticeshow
-
noticehide
-
destroy
-
lrcshow
-
lrchide
LRC
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.
LRC file
The first way, put LRC to a LRC file, LRC file will be loaded when this audio start to play.
1 | const ap = new APlayer({ |
LRC string in JS
The second way, put LRC to a JS string.
1 | const ap = new APlayer({ |
LRC in HTML
The third way, put LRC to HTML.
1 | <link rel="stylesheet" href="APlayer.min.css"> |
1 | const ap = new APlayer({ |
LRC format
The following LRC format are supported:
[mm:ss]APlayer
[mm:ss.xx]is
[mm:ss.xxx]amazing
[mm:ss.xx][mm:ss.xx]APlayer
[mm:ss.xx]<mm:ss.xx>is
[mm:ss.xx]amazing[mm:ss.xx]APlayer
Playlist
APlayer will show a playlist when it has more than one audio, option listFolded indicates whether list should folded at first, and option listMaxHeight indicates list max height.
1 | const ap = new APlayer({ |
Fixed mode
APlayer can be fixed to page bottom via fixed mode, fixed mode is a very different mode, enjoy it!
1 | const ap = new APlayer({ |
Mini mode
If you don’t have enough space for normal player, mini mode might be your choice.
Please note that mini mode is conflicted with fixed mode.
1 | const ap = new APlayer({ |
MSE support
HLS
It requires the library hls.js and it should be loaded before APlayer.min.js.
1 | <link rel="stylesheet" href="APlayer.min.css"> |
1 | const ap = new APlayer({ |
1 | // another way, use customType |
Self-adapting theme according to cover
It requires the library color-thief.
1 | <link rel="stylesheet" href="APlayer.min.css"> |
1 | const ap = new APlayer({ |
CDN
FAQ
Why can’t player autoplay in some mobile browsers?
Most mobile browsers forbid audio autoplay, you wont be able to achieve it without hacks.