Plyr

一个简单的 HTML5、YouTube 和 Vimeo 播放器。「A simple HTML5, YouTube and Vimeo player」

Github stars Tracking Chart

Plyr 是一款简单、轻量级、可访问和可定制的 HTML5、YouTube 和 Vimeo 媒体播放器,支持现代浏览器。

查看演示 - 捐赠 - Slack

特性

  • HTML 视频和音频,YouTube 和 Vimeo -- 支持主要格式。
  • Accessible -- 完全支持 VTT 字幕和屏幕阅读器。
  • 可定制 -- 用你想要的标记使播放器看起来如何。
  • 干净的HTML -- 使用正确的元素。<input type="range"> 代表音量,<progress> 代表进度,<button>代表按钮。没有或 <a href="#"> 按钮黑客。
  • 响应式 -- 适用于任何屏幕尺寸。
  • 货币化 -- 从你的视频中赚钱。
  • 流媒体 -- 支持 hls.js、Shaka 和 dash.js 流媒体播放。
  • API -- 通过标准化的 API 来切换播放、音量、寻找等功能。
  • 事件 -- 无需在 Vimeo 和 YouTube 的 API 上做手脚,所有事件都是跨格式的标准化事件。
  • Fullscreen -- 支持原生全屏,并可回退到 "全窗口 "模式。
  • 键盘快捷键 -- 支持键盘快捷键。
  • Picture-in-Picture -- 支持画中画模式。
  • Playsinline -- 支持 playsinline 属性。
  • 速度控制 -- 在飞行中调整速度。
  • 多重字幕 -- 支持多重字幕轨道
  • i18n 支持 -- 支持管制的国际化。
  • 预览缩略图 -- 支持显示预览缩略图。
  • 没有框架- -- 用 "vanilla" ES6 JavaScript 编写,不需要 jQuery。
  • SASS -- 包含在你的构建过程中。

演示

。HTML5 视频、HTML5 音频、YouTube、Vimeo。Dash.js、Hls.js 和 Shaka Player。Dash.js, Hls.js 和 Shaka Player。

你可以使用我们的最小模板在 Codepen 中尝试 Plyr:HTML5 video, HTML5 audio, YouTube, Vimeo。对于流媒体,我们还提供了与以下内容的集成示例:Dash.js, Hls.jsShaka Player


Main metrics

Overview
Name With Ownersampotts/plyr
Primary LanguageJavaScript
Program languageJavaScript (Language Count: 2)
PlatformWeb browsers
License:MIT License
所有者活动
Created At2015-02-14 11:33:07
Pushed At2024-06-29 18:16:28
Last Commit At2024-06-28 17:47:20
Release Count235
Last Release Namev3.7.8 (Posted on 2023-03-24 23:03:03)
First Release Namev1.0.0 (Posted on )
用户参与
Stargazers Count28.8k
Watchers Count433
Fork Count3.1k
Commits Count2.3k
Has Issues Enabled
Issues Count2103
Issue Open Count882
Pull Requests Count384
Pull Requests Open Count36
Pull Requests Close Count137
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private

Plyr is a simple, lightweight, accessible and customizable HTML5, YouTube and Vimeo media player that supports modern browsers.

Checkout the demo - Donate - Slack - npm version Gitpod Ready-to-Code

Image of Plyr

Features

  • ? HTML Video & Audio, YouTube & Vimeo - support for the major formats
  • ? Accessible - full support for VTT captions and screen readers
  • ? Customizable - make the player look how you want with the markup you want
  • ? Clean HTML - uses the right elements. <input type="range"> for volume and <progress> for progress and well, <button>s for buttons. There's no
    <span> or <a href="#"> button hacks
  • ? Responsive - works with any screen size
  • ? Monetization - make money from your videos
  • ? Streaming - support for hls.js, Shaka and dash.js streaming playback
  • ? API - toggle playback, volume, seeking, and more through a standardized API
  • ? Events - no messing around with Vimeo and YouTube APIs, all events are standardized across formats
  • ? Fullscreen - supports native fullscreen with fallback to "full window" modes
  • ⌨️ Shortcuts - supports keyboard shortcuts
  • ? Picture-in-Picture - supports picture-in-picture mode
  • ? Playsinline - supports the playsinline attribute
  • ? Speed controls - adjust speed on the fly
  • ? Multiple captions - support for multiple caption tracks
  • ? i18n support - support for internationalization of controls
  • ? Preview thumbnails - support for displaying preview thumbnails
  • ? No frameworks - written in "vanilla" ES6 JavaScript, no jQuery required
  • ?‍♀️ SASS - to include in your build processes

Demos

You can try Plyr in Codepen using our minimal templates: HTML5 video, HTML5 audio, YouTube, Vimeo. For Streaming we also have example integrations with: Dash.js, Hls.js and Shaka Player

Quick setup

HTML

Plyr extends upon the standard HTML5 media element markup so that's all you need for those types.

HTML5 Video

<video poster="/path/to/poster.jpg" id="player" playsinline controls>
    <source src="/path/to/video.mp4" type="video/mp4" />
    <source src="/path/to/video.webm" type="video/webm" />

    <!-- Captions are optional -->
    <track kind="captions" label="English captions" src="/path/to/captions.vtt" srclang="en" default />
</video>

HTML5 Audio

<audio id="player" controls>
    <source src="/path/to/audio.mp3" type="audio/mp3" />
    <source src="/path/to/audio.ogg" type="audio/ogg" />
</audio>

For YouTube and Vimeo players, Plyr uses progressive enhancement to enhance the default <iframe> embeds. Below are some examples. The plyr__video-embed classname will make the embed responsive. You can add the autoplay, loop, hl (YouTube only) and playsinline (YouTube only) query parameters to the URL and they will be set as config options automatically. For YouTube, the origin should be updated to reflect the domain you're hosting the embed on, or you can opt to omit it.

YouTube

We recommend progressive enhancement with the embedded players. You can elect to use an <iframe> as the source element (which Plyr will progressively enhance) or a bog standard <div> with two essential data attributes - data-plyr-provider and data-plyr-embed-id.

<div class="plyr__video-embed" id="player">
    <iframe
        src="https://www.youtube.com/embed/bTqVqk7FSmY?origin=https://plyr.io&amp;iv_load_policy=3&amp;modestbranding=1&amp;playsinline=1&amp;showinfo=0&amp;rel=0&amp;enablejsapi=1"
        allowfullscreen
        allowtransparency
        allow="autoplay"
    ></iframe>
</div>

Note: The plyr__video-embed classname will make the player a responsive 16:9 (most common) iframe embed. When plyr itself kicks in, your custom ratio config option will be used.

Or the <div> non progressively enhanced method:

<div id="player" data-plyr-provider="youtube" data-plyr-embed-id="bTqVqk7FSmY"></div>

Note: The data-plyr-embed-id can either be the video ID or URL for the media.

Vimeo

Much the same as YouTube above.

<div class="plyr__video-embed" id="player">
    <iframe
        src="https://player.vimeo.com/video/76979871?loop=false&amp;byline=false&amp;portrait=false&amp;title=false&amp;speed=true&amp;transparent=0&amp;gesture=media"
        allowfullscreen
        allowtransparency
        allow="autoplay"
    ></iframe>
</div>

Or the <div> non progressively enhanced method:

<div id="player" data-plyr-provider="vimeo" data-plyr-embed-id="76979871"></div>

JavaScript

You can use Plyr as an ES6 module as follows:

import Plyr from 'plyr';

const player = new Plyr('#player');

Alternatively you can include the plyr.js script before the closing </body> tag and then in your JS create a new instance of Plyr as below.

<script src="path/to/plyr.js"></script>
<script>
    const player = new Plyr('#player');
</script>

See initialising for more information on advanced setups.

You can use our CDN (provided by Fastly) for the JavaScript. There's 2 versions; one with and one without polyfills. My recommendation would be to manage polyfills seperately as part of your application but to make life easier you can use the polyfilled build.

<script src="https://cdn.plyr.io/3.5.10/plyr.js"></script>

...or...

<script src="https://cdn.plyr.io/3.5.10/plyr.polyfilled.js"></script>

CSS

Include the plyr.css stylsheet into your <head>.

<link rel="stylesheet" href="path/to/plyr.css" />

If you want to use our CDN (provided by Fastly) for the default CSS, you can use the following:

<link rel="stylesheet" href="https://cdn.plyr.io/3.5.10/plyr.css" />

SVG Sprite

The SVG sprite is loaded automatically from our CDN (provided by Fastly). To change this, see the options below. For
reference, the CDN hosted SVG sprite can be found at https://cdn.plyr.io/3.5.10/plyr.svg.

Ads

Plyr has partnered up with vi.ai to offer monetization options for your videos. Getting setup is easy:

Any questions regarding the ads can be sent straight to vi.ai and any issues with rendering raised through GitHub issues.

Advanced

SASS

You can use bundle.scss file included in /src as part of your build and change variables to suit your design. The SASS require you to
use the autoprefixer plugin (you should be already!) as all declarations use the W3C definitions.

The HTML markup uses the BEM methodology with plyr as the block, e.g. .plyr__controls. You can change the class hooks in the options to match any custom CSS
you write. Check out the JavaScript source for more on this.

SVG

The icons used in the Plyr controls are loaded in an SVG sprite. The sprite is automatically loaded from our CDN by default. If you already have an icon build
system in place, you can include the source plyr icons (see /src/sprite for source icons).

Using the iconUrl option

You can however specify your own iconUrl option and Plyr will determine if the url is absolute and requires loading by AJAX/CORS due to current browser
limitations or if it's a relative path, just use the path directly.

If you're using the <base> tag on your site, you may need to use something like this: svgfixer.js

More info on SVG sprites here: http://css-tricks.com/svg-sprites-use-better-icon-fonts/ and the AJAX
technique here: http://css-tricks.com/ajaxing-svg-sprite/

Cross Origin (CORS)

You'll notice the crossorigin attribute on the example <video> elements. This is because the TextTrack captions are loaded from another domain. If your
TextTrack captions are also hosted on another domain, you will need to add this attribute and make sure your host has the correct headers setup. For more info
on CORS checkout the MDN docs:
https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

Captions

WebVTT captions are supported. To add a caption track, check the HTML example above and look for the <track> element. Be sure to
validate your caption files.

JavaScript

Initialising

You can specify a range of arguments for the constructor to use:

Note: If a NodeList, Array, or jQuery object are passed, the first element will be used for setup. To setup multiple players, see multiple players below.

Single player

Passing a CSS string selector that's compatible with querySelector:

const player = new Plyr('#player');

Passing a HTMLElement:

const player = new Plyr(document.getElementById('player'));
const player = new Plyr(document.querySelector('.js-player'));

The HTMLElement or string selector can be the target <video>, <audio>, or <div> wrapper for embeds.

Multiple players

You have two choices here. You can either use a simple array loop to map the constructor:

const players = Array.from(document.querySelectorAll('.js-player')).map(p => new Plyr(p));

...or use a static method where you can pass a CSS string selector, a NodeList, an Array of HTMLElement, or a JQuery object:

const players = Plyr.setup('.js-player');

Both options will also return an array of instances in the order of they were in the DOM for the string selector or the source NodeList or Array.

Options

The second argument for the constructor is the options object:

const player = new Plyr('#player', {
    title: 'Example Title',
});

Options can be passed as an object to the constructor as above or as JSON in data-plyr-config attribute on each of your target elements:

<video src="/path/to/video.mp4" id="player" controls data-plyr-config='{ "title": "Example Title" }'></video>

Note the single quotes encapsulating the JSON and double quotes on the object keys. Only string values need double quotes.