DPlayer/README.md

217 lines
7.5 KiB
Markdown
Raw Normal View History

2016-05-03 14:11:47 +00:00
# DPlayer
[![CDNJS](https://img.shields.io/cdnjs/v/dplayer.svg?style=flat-square)](https://cdnjs.com/libraries/dplayer)
2016-05-03 14:11:47 +00:00
[![npm](https://img.shields.io/npm/v/dplayer.svg?style=flat-square)](https://www.npmjs.com/package/dplayer)
2016-07-14 03:14:15 +00:00
[![npm](https://img.shields.io/npm/l/dplayer.svg?style=flat-square)](https://github.com/DIYgod/DPlayer/blob/master/LICENSE)
2016-05-03 14:11:47 +00:00
[![devDependency Status](https://img.shields.io/david/dev/DIYgod/dplayer.svg?style=flat-square)](https://david-dm.org/DIYgod/DPlayer#info=devDependencies)
[![npm](https://img.shields.io/npm/dt/dplayer.svg?style=flat-square)](https://www.npmjs.com/package/dplayer)
[![Travis](https://img.shields.io/travis/DIYgod/DPlayer.svg?style=flat-square)](https://travis-ci.org/DIYgod/DPlayer)
2016-08-18 10:41:49 +00:00
[![gitter](https://img.shields.io/gitter/room/DIYgod/DPlayer.svg?style=flat-square)](https://gitter.im/diygod233/dplayer)
2016-05-03 14:11:47 +00:00
[![%e2%9d%a4](https://img.shields.io/badge/made%20with-%e2%9d%a4-ff69b4.svg?style=flat-square)](https://www.anotherhome.net/)
2016-05-17 18:23:44 +00:00
> Wow, such a lovely HTML5 danmaku video player
2016-05-03 14:11:47 +00:00
## Introduction
2016-07-14 03:14:15 +00:00
[Demo](http://dplayer.js.org/)
2016-10-17 03:15:10 +00:00
Using DPlayer on your project? [Let me know!](https://github.com/DIYgod/DPlayer/issues/31)
2016-10-17 03:14:21 +00:00
2016-05-20 00:48:30 +00:00
Screenshot
2016-05-20 13:31:17 +00:00
![image](http://i.imgur.com/207ch36.jpg)
2016-05-20 00:48:30 +00:00
2016-05-03 14:11:47 +00:00
## Install
```
$ npm install dplayer --save
2016-05-03 14:14:11 +00:00
```
2016-05-03 14:11:47 +00:00
## Usage
### HTML
```HTML
<div id="player1" class="dplayer"></div>
<!-- ... -->
2016-08-02 09:56:45 +00:00
<script src="dist/DPlayer.min.js"></script>
```
### JS
```JS
var dp = new DPlayer(option);
```
2016-05-20 02:38:36 +00:00
**Options**
```JS
var option = {
element: document.getElementById('player1'), // Optional, player element
autoplay: false, // Optional, autoplay video, not supported by mobile browsers
theme: '#FADFA3', // Optional, theme color, default: #b7daff
loop: true, // Optional, loop play music, default: true
2016-07-20 06:45:26 +00:00
lang: 'zh', // Optional, language, `zh` for Chinese, `en` for English, default: Navigator language
screenshot: true, // Optional, enable screenshot function, default: false, NOTICE: if set it to true, video and video poster must enable Cross-Origin
2016-08-22 12:07:31 +00:00
hotkey: true, // Optional, binding hot key, including left right and Space, default: true
2016-08-28 14:53:58 +00:00
preload: 'auto', // Optional, the way to load music, can be 'none' 'metadata' 'auto', default: 'auto'
video: { // Required, video info
url: '若能绽放光芒.mp4', // Required, video url
pic: '若能绽放光芒.png' // Optional, music picture
},
2016-08-02 09:56:45 +00:00
danmaku: { // Optional, showing danmaku, ignore this option to hide danmaku
id: '9E2E3368B56CDBB4', // Required, danmaku id, NOTICE: it must be unique, can not use these in your new player: `https://dplayer.daoapp.io/list`
api: 'https://dplayer.daoapp.io/', // Required, danmaku api
token: 'tokendemo', // Optional, danmaku token for api
2016-10-11 17:03:36 +00:00
maximum: 1000, // Optional, maximum quantity of danmaku
addition: ['https://dplayer.daoapp.io/bilibili?aid=4157142'] // Optional, additional danmaku, see: `Bilibili 弹幕支持`
}
}
```
2016-05-20 02:38:36 +00:00
**API**
2016-07-15 02:33:20 +00:00
+ `dp.play()` // Resume play
+ `dp.play(time)` // Set currentTime
+ `dp.pause()` // Pause
+ `dp.toggle()` // Toggle between play and pause
+ `dp.on(event, handler)` // Event binding
2016-10-08 03:16:01 +00:00
+ `dp.switchVideo(video, danmaku)` // Switch to a new video, the format of `video` and `danmaku` is the same as option
+ `dp.dan` // Return danmaku info
+ `dp.danIndex` // Return danmaku index
+ `dp.video` // Return native video, most [native api](http://www.w3schools.com/tags/ref_av_dom.asp) are supported
+ `dp.video.currentTime` // Returns the current playback position
+ `dp.video.loop` // Returns whether the video should start over again when finished
+ Most [native api](http://www.w3schools.com/tags/ref_av_dom.asp)
2016-05-20 02:38:36 +00:00
**Event binding**
`dp.on(event, handler)`
`event`:
+ `play`: Triggered when DPlayer start play
+ `pause`: Triggered when DPlayer paused
+ `canplay`: Triggered when enough data is available that DPlayer can be played
+ `playing`: Triggered periodically when DPlayer is playing
+ `ended`: Triggered when DPlayer ended
+ `error`: Triggered when an error occurs
**bilibili 弹幕及直链支持**
弹幕
API: `https://dplayer.daoapp.io/bilibili?aid=【bilibili视频AV号】`
```JS
var option = {
danmaku: {
// ...
addition: ['https://dplayer.daoapp.io/bilibili?aid=【bilibili视频AV号】']
}
}
```
直链
API: `https://dplayer.daoapp.io/video/bilibili?aid=【bilibili视频AV号】`
```JS
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
    if (xhr.readyState === 4) {
        if (xhr.status >= 200 && xhr.status < 300 || xhr.status === 304) {
            var response = JSON.parse(xhr.responseText);
            var dp1 = new DPlayer({
                // options
                screenshot: false,
                video: {
                    url: response.durl[0].url
                    // options
                }
            });
        }
        else {
            console.log('Request was unsuccessful: ' + xhr.status);
        }
    }
};
xhr.open('get', 'https://dplayer.daoapp.io/video/bilibili?aid=【bilibili视频AV号】', true);
xhr.send(null);
```
2016-08-02 09:56:45 +00:00
**Live Video (HTTP Live Streaming, M3U8 format)**
It requires the library [hls.js](https://github.com/dailymotion/hls.js) and it should be loaded before DPlayer.min.js.
Live danamku is not supported yet.
2016-08-02 09:56:45 +00:00
```HTML
<div id="player1" class="dplayer"></div>
<!-- ... -->
<script src="plugin/hls.min.js"></script>
<script src="dist/DPlayer.min.js"></script>
<script>
var dp = new DPlayer({
// ...
video: {
url: 'xxx.m3u8'
// ...
}
});
</script>
```
2016-05-20 02:38:36 +00:00
**Work with module bundler**
```js
var DPlayer = require('DPlayer');
var dp = new DPlayer(option);
```
2016-05-20 02:33:55 +00:00
### Danmaku back-end
**Ready-made API:**
`https://dplayer.daoapp.io/`
**Build yourself:**
Required environment
- Node.js
- Mongodb
2016-05-03 14:11:47 +00:00
## Run in development
```
$ npm install
$ npm run dev
```
## Make a release
```
$ npm install
$ npm run build
```
2016-05-13 00:43:39 +00:00
## Related Projects
- [DPlayer-for-typecho](https://github.com/volio/DPlayer-for-typecho)
2016-07-14 03:14:15 +00:00
2016-05-23 16:20:43 +00:00
- [Hexo-tag-dplayer](https://github.com/NextMoe/hexo-tag-dplayer)
2016-07-14 03:14:15 +00:00
2016-06-01 15:05:13 +00:00
- [DPlayer_for_Z-BlogPHP](https://github.com/fghrsh/DPlayer_for_Z-BlogPHP)
2016-05-13 00:43:39 +00:00
2016-08-24 03:02:31 +00:00
- [纸飞机视频区插件(DPlayer for Discuz!)](https://coding.net/u/Click_04/p/video/git)
2016-09-08 17:17:24 +00:00
- [dplayer_py_backend](https://github.com/dixyes/dplayer_py_backend)
- [dplayer_lua_backend](https://github.com/dixyes/dplayer_lua_backend)
2016-09-19 01:51:56 +00:00
- [DPlayer for WordPress](https://github.com/BlueCocoa/DPlayer-WordPress)
2016-05-03 14:11:47 +00:00
## LICENSE
[The Star And Thank Author License (SATA)](https://github.com/DIYgod/DPlayer/blob/master/LICENSE)