作者:陈小超Sum | 来源:互联网 | 2023-08-08 18:47
先上效果InstallationUsingnpm:npminstalldplayer--saveUsingYarn:yarnadddplayerQuickStartAtfi
先上效果
Installation
Using npm:
npm install dplayer --save
Using Yarn:
yarn add dplayer
Quick Start
At first, let's initialize a simplest DPlayer
Load DPlayer files
Or work with module bundler:
import DPlayer from 'dplayer';const dp = new DPlayer(options);
Initialization in js:
const dp = new DPlayer({container: document.getElementById('dplayer'),screenshot: true,video: {url: 'demo.mp4',pic: 'demo.jpg',thumbnails: 'thumbnails.jpg',},subtitle: {url: 'webvtt.vtt',},danmaku: {id: 'demo',api: 'https://api.prprpr.me/dplayer/',},
});
Options
You can custom your player instance by those options
Name | Default | Description |
---|
container | document.querySelector('.dplayer') | player container |
live | false | enable live mode, see #live |
autoplay | false | video autoplay |
theme | '#b7daff' | main color |
loop | false | video loop |
lang | navigator.language.toLowerCase() | values: 'en', 'zh-cn', 'zh-tw' |
screenshot | false | enable screenshot, if true, video and video poster must enable Cross-Origin |
airplay | true | enable airplay in Safari |
hotkey | true | enable hotkey, support FF, FR, volume control, play & pause |
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 |
playbackSpeed | [0.5, 0.75, 1, 1.25, 1.5, 2] | optional playback speed, or or you can set a custom one |
logo | - | showing logo in the top left corner, you can adjust its size and position by CSS |
apiBackend | - | getting and sending danmaku in your way, see #live |
video | - | video info |
video.quality | - | see #Quality switching |
video.defaultQuality | - | see #Quality switching |
video.url | - | video url |
video.pic | - | video poster |
video.thumbnails | - | video thumbnails, generated by DPlayer-thumbnails(opens new window) |
video.type | 'auto' | values: 'auto', 'hls', 'flv', 'dash', 'webtorrent', 'normal' or other custom type, see #MSE support |
video.customType | - | custom video type, see #MSE support |
subtitle | - | external subtitle |
subtitle.url | required | subtitle url |
subtitle.type | 'webvtt' | subtitle type, values: 'webvtt', 'ass', but only webvtt is supported for now |
subtitle.fontSize | '20px' | subtitle font size |
subtitle.bottom | '40px' | the distance between the subtitle and player bottom, values like: '10px' '10%' |
subtitle.color | '#fff' | subtitle color |
danmaku | - | showing danmaku |
danmaku.id | required | danmaku pool id, it must be unique |
danmaku.api | required | see #Danmaku API |
danmaku.token | - | back end verification token |
danmaku.maximum | - | danmaku maximum quantity |
danmaku.addition | - | additional danmaku, see #bilibili danmaku |
danmaku.user | 'DIYgod' | danmaku user name |
danmaku.bottom | - | values like: '10px' '10%', the distance between the danmaku bottom and player bottom, in order to prevent warding off subtitle |
danmaku.unlimited | false | display all danmaku even though danmaku overlap, notice that player will remember user setting, default setting will not work after user set it themselves |
contextmenu | [] | custom contextmenu |
highlight | [] | custom time markers upon progress bar |
mutex | true | prevent to play multiple player at the same time, pause other players when this player start play |
const dp = new DPlayer({container: document.getElementById('dplayer'),autoplay: false,theme: '#FADFA3',loop: true,lang: 'zh-cn',screenshot: true,hotkey: true,preload: 'auto',logo: 'logo.png',volume: 0.7,mutex: true,video: {url: 'dplayer.mp4',pic: 'dplayer.png',thumbnails: 'thumbnails.jpg',type: 'auto',},subtitle: {url: 'dplayer.vtt',type: 'webvtt',fontSize: '25px',bottom: '10%',color: '#b7daff',},danmaku: {id: '9E2E3368B56CDBB4',api: 'https://api.prprpr.me/dplayer/',token: 'tokendemo',maximum: 1000,addition: ['https://api.prprpr.me/dplayer/v3/bilibili?aid=4157142'],user: 'DIYgod',bottom: '15%',unlimited: true,},contextmenu: [{text: 'custom1',link: 'https://github.com/DIYgod/DPlayer',},{text: 'custom2',click: (player) => {console.log(player);},},],highlight: [{text: 'marker for 20s',time: 20,},{text: 'marker for 2mins',time: 120,},],
});
查看插件地址:传送门