站点图标 度崩网-几度崩溃

DPlayer播放器指南-such a lovely HTML5 danmaku video player

 安装

使用 npm:
npm install dplayer --save

使用 Yarn:
yarn add dplayer


快速开始

我们先尝试初始化一个最简单的 DPlayer
加载播放器文件:
<link rel="stylesheet" href="DPlayer.min.css" />
<div id="dplayer"></div>
<script src="DPlayer.min.js"></script>

或者使用模块管理器:
import 'dplayer/dist/DPlayer.min.css';
import DPlayer from 'dplayer';

const dp = new DPlayer(options);

在 js 里初始化:
const dp = new DPlayer({
    container: document.getElementById('dplayer'),
    video: {
        url: 'demo.mp4',
    },
});

一个最简单的 DPlayer 就初始化好了,它只有最基本的视频播放功能


参数

DPlayer 有丰富的参数可以自定义你的播放器实例
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: [
        {
            time: 20,
            text: '这是第 20 秒',
        },
        {
            time: 120,
            text: '这是 2 分钟',
        },
    ],
});


API


事件绑定

dp.on(event, handler)
dp.on('ended', function() {
    console.log('player ended');
});

视频事件
播放器事件


清晰度切换

在 video.quality 里设置不同清晰度的视频链接和类型,video.defaultQuality 设置默认清晰度
Load demo
const dp = new DPlayer({
    container: document.getElementById('dplayer'),
    video: {
        quality: [
            {
                name: 'HD',
                url: 'demo.m3u8',
                type: 'hls',
            },
            {
                name: 'SD',
                url: 'demo.mp4',
                type: 'normal',
            },
        ],
        defaultQuality: 0,
        pic: 'demo.png',
        thumbnails: 'thumbnails.jpg',
    },
});


弹幕


#
弹幕接口
danmaku.api
现成的接口
链接: https://api.prprpr.me/dplayer/
每日备份: DPlayer-data
自己搭建
DPlayer-node

#
bilibili 弹幕
danmaku.addition
API: https://api.prprpr.me/dplayer/v3/bilibili?aid=[aid]
const option = {
    danmaku: {
        // ...
        addition: ['https://api.prprpr.me/dplayer/v3/bilibili?aid=[aid]'],
    },
};


MSE 支持


#
HLS
需要在 DPlayer.min.js 前面加载 hls.js
Load demo
<link rel="stylesheet" href="DPlayer.min.css" />
<div id="dplayer"></div>
<script src="hls.min.js"></script>
<script src="DPlayer.min.js"></script>

const dp = new DPlayer({ container: document.getElementById('dplayer'), video: { url: 'demo.m3u8', type: 'hls', }, pluginOptions: { hls: { // hls config }, }, }); console.log(dp.plugins.hls); // Hls 实例
// 另一种方式,使用 customType const dp = new DPlayer({ container: document.getElementById('dplayer'), video: { url: 'demo.m3u8', type: 'customHls', customType: { customHls: function(video, player) { const hls = new Hls(); hls.loadSource(video.src); hls.attachMedia(video); }, }, }, });

#
MPEG DASH
需要在 DPlayer.min.js 前面加载 dash.js
<link rel="stylesheet" href="DPlayer.min.css" />
<div id="dplayer"></div>
<script src="dash.min.js"></script>
<script src="DPlayer.min.js"></script>

const dp = new DPlayer({ container: document.getElementById('dplayer'), video: { url: 'demo.mpd', type: 'dash', }, pluginOptions: { dash: { // dash config }, }, }); console.log(dp.plugins.dash); // Dash 实例
// 另一种方式,使用 customType const dp = new DPlayer({ container: document.getElementById('dplayer'), video: { url: 'demo.mpd', type: 'customDash', customType: { customDash: function(video, player) { dashjs .MediaPlayer() .create() .initialize(video, video.src, false); }, }, }, });

#
MPEG DASH (Shaka)
需要在 DPlayer.min.js 前面加载 shaka-player.compiled.js
<link rel="stylesheet" href="DPlayer.min.css" />
<div id="dplayer"></div>
<script src="shaka-player.compiled.js"></script>
<script src="DPlayer.min.js"></script>

const dp = new DPlayer({ container: document.getElementById('dplayer'), screenshot: true, video: { url: 'demo.mpd', type: 'shakaDash', customType: { shakaDash: function(video, player) { var src = video.src; var playerShaka = new shaka.Player(video); // 将会修改 video.src playerShaka.load(src); }, }, }, });

#
FLV
需要在 DPlayer.min.js 前面加载 flv.js
Load demo
<link rel="stylesheet" href="DPlayer.min.css" />
<div id="dplayer"></div>
<script src="flv.min.js"></script>
<script src="DPlayer.min.js"></script>

const dp = new DPlayer({ container: document.getElementById('dplayer'), video: { url: 'demo.flv', type: 'flv', }, pluginOptions: { flv: { // flv config }, }, }); console.log(dp.plugins.flv); // flv 实例
// 另一种方式,使用 customType const dp = new DPlayer({ container: document.getElementById('dplayer'), video: { url: 'demo.flv', type: 'customFlv', customType: { customFlv: function(video, player) { const flvPlayer = flvjs.createPlayer({ type: 'flv', url: video.src, }); flvPlayer.attachMediaElement(video); flvPlayer.load(); }, }, }, });

#
WebTorrent
需要在 DPlayer.min.js 前面加载 webtorrent
Load demo
<link rel="stylesheet" href="DPlayer.min.css" />
<div id="dplayer"></div>
<script src="webtorrent.min.js"></script>
<script src="DPlayer.min.js"></script>

const dp = new DPlayer({ container: document.getElementById('dplayer'), video: { url: 'magnet:demo', type: 'webtorrent', }, pluginOptions: { webtorrent: { // webtorrent config }, }, }); console.log(dp.plugins.webtorrent); // WebTorrent 实例
// 另一种方式,使用 customType const dp = new DPlayer({ container: document.getElementById('dplayer'), video: { url: 'magnet:demo', type: 'customWebTorrent', customType: { customWebTorrent: function(video, player) { player.container.classList.add('dplayer-loading'); const client = new WebTorrent(); const torrentId = video.src; client.add(torrentId, (torrent) => { const file = torrent.files.find((file) => file.name.endsWith('.mp4')); file.renderTo( video, { autoplay: player.options.autoplay, }, () => { player.container.classList.remove('dplayer-loading'); } ); }); }, }, }, });

#
配合其他 MSE 库使用
DPlayer 可以通过 customType 参数与任何 MSE 库一起使用
<link rel="stylesheet" href="DPlayer.min.css" />
<div id="dplayer"></div>
<script src="pearplayer.js"></script>
<script src="DPlayer.min.js"></script>

const dp = new DPlayer({ container: document.getElementById('dplayer'), video: { url: 'https://qq.webrtc.win/tv/Pear-Demo-Yosemite_National_Park.mp4', type: 'pearplayer', customType: { pearplayer: function(video, player) { new PearPlayer(video, { src: video.src, autoplay: player.options.autoplay, }); }, }, }, });


直播

你可以把 DPlayer 用在直播当中,但如果你想要直播弹幕,你需要自己准备一个 WebSocket 后端。
Load demo
初始化播放器:
const dp = new DPlayer({
    container: document.getElementById('dplayer'),
    live: true,
    danmaku: true,
    apiBackend: {
        read: function(endpoint, callback) {
            console.log('Pretend to connect WebSocket');
            callback();
        },
        send: function(endpoint, danmakuData, callback) {
            console.log('Pretend to send danamku via WebSocket', danmakuData);
            callback();
        },
    },
    video: {
        url: 'demo.m3u8',
        type: 'hls',
    },
});

通过 WebSocket 获取到弹幕之后,通过 dp.danmaku.draw 绘制弹幕:
const danmaku = {
    text: 'Get a danamku via WebSocket',
    color: '#fff',
    type: 'right',
};
dp.danmaku.draw(danmaku);


常见问题


#
为什么播放器不能全屏?
如果播放器被包含在 iframe 里,尝试在 iframe 上添加 allowfullscreen 属性。
为了完善的浏览器兼容性,它应该是这样:
<iframe src="example.com" allowfullscreen="allowfullscreen" mozallowfullscreen="mozallowfullscreen" msallowfullscreen="msallowfullscreen" oallowfullscreen="oallowfullscreen" webkitallowfullscreen="webkitallowfullscreen"></iframe>


#
为什么播放器不能在手机上自动播放?
大多数移动端浏览器禁止了视频自动播放。