APlayer
🍭 Wow, such a beautiful HTML5 music player
Special Sponsors
Installation
Using npm:
1 | |
Using Yarn:
1 | |
Quick Start
1 | |
1 | |
Work with module bundler:
1 | |
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 | |
API
APlayer.version: static property, return the version of APlayerap.play(): play audioap.pause(): pause audioap.seek(time: number): seek to specified time, the unit of time is second1
ap.seek(100);ap.toggle(): toggle between play and pauseap.on(event: string, handler: function): bind audio and player events, see more detailsap.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 audioap.skipForward(): skip to next audioap.destroy(): destroy playerap.lrcap.lrc.show(): show lrcap.lrc.hide(): hide lrcap.lrc.toggle(): toggle lrc between show and hide
ap.listap.list.show(): show listap.list.hide(): hide listap.list.toggle(): toggle list between show and hideap.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 audioap.audio.currentTime: returns the current playback positionap.audio.duration: returns audio total timeap.audio.paused: returns whether the audio pausedmost native api are supported
Event binding
ap.on(event, handler)
1 | |
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 | |
LRC string in JS
The second way, put LRC to a JS string.
1 | |
LRC in HTML
The third way, put LRC to HTML.
1 | |
1 | |
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 | |
Fixed mode
APlayer can be fixed to page bottom via fixed mode, fixed mode is a very different mode, enjoy it!
1 | |
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 | |
MSE support
HLS
It requires the library hls.js and it should be loaded before APlayer.min.js.
1 | |
1 | |
1 | |
Self-adapting theme according to cover
It requires the library color-thief.
1 | |
1 | |
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.