0.0.1 • Published 6 years ago

asyncy-player v0.0.1

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

Plyr

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

Checkout the demo - Donate to support Plyr - Chat on Slack

Image of Plyr

Features

  • Accessible - full support for VTT captions and screen readers
  • Customisable - make the player look how you want with the markup you want
  • Good 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
  • HTML Video & Audio - support for both formats
  • Embedded Video - support for YouTube and Vimeo video playback
  • 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 Safari's 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
  • No dependencies - written in "vanilla" ES6 JavaScript, no jQuery required
  • SASS - to include in your build processes

Oh and yes, it works with Bootstrap.

Changelog

Check out the changelog to see what's new with Plyr.

Plugins & Components

Some awesome folks have made plugins for CMSs and Components for JavaScript frameworks:

TypeMaintainerLink
WordPressBrandon Lavigne (@drrobotnik)https://wordpress.org/plugins/plyr/
ReactJose Miguel Bejarano (@xDae)https://github.com/xDae/react-plyr
VueGabe Dunn (@redxtech)https://github.com/redxtech/vue-plyr
NeosJon Uhlmann (@jonnitto)https://packagist.org/packages/jonnitto/plyr
KirbyDominik Pschenitschni (@dpschen)https://github.com/dpschen/kirby-plyrtag

Quick setup

Here's a quick run through on getting up and running. There's also a demo on Codepen. You can grab all of the source with NPM using npm install plyr.

Try Plyr online

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

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 embed

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 embed

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

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.4.3/plyr.js"></script>

...or...

<script src="https://cdn.plyr.io/3.4.3/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.4.3/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.4.3/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 be should 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 setting up multiple players below.

Here's some examples

Passing a string selector:

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

Passing a HTMLElement:

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

Passing a NodeList (see note below):

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

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

Setting up 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 string selector, a NodeList or an Array of elements:

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.

Passing options

The second argument for the constructor is the options object:

const player = new Plyr('#player', {
    /* options */
});

In all cases, the constructor will return a Plyr object that can be used with the API methods. See the API section for more info.

Options

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": "This is an example video", "volume": 1, "debug": true }'></video>

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

OptionTypeDefaultDescription
enabledBooleantrueCompletely disable Plyr. This would allow you to do a User Agent check or similar to programmatically enable or disable Plyr for a certain UA. Example below.
debugBooleanfalseDisplay debugging information in the console
controlsArray, Function or Element['play-large', 'play', 'progress', 'current-time', 'mute', 'volume', 'captions', 'settings', 'pip', 'airplay', 'fullscreen']If a function is passed, it is assumed your method will return either an element or HTML string for the controls. Three arguments will be passed to your function; id (the unique id for the player), seektime (the seektime step in seconds), and title (the media title). See controls.md for more info on how the html needs to be structured.
settingsArray['captions', 'quality', 'speed', 'loop']If you're using the default controls are used then you can specify which settings to show in the menu
i18nObjectSee defaults.jsUsed for internationalization (i18n) of the text within the UI.
loadSpriteBooleantrueLoad the SVG sprite specified as the iconUrl option (if a URL). If false, it is assumed you are handling sprite loading yourself.
iconUrlStringnullSpecify a URL or path to the SVG sprite. See the SVG section for more info.
iconPrefixStringplyrSpecify the id prefix for the icons used in the default controls (e.g. "plyr-play" would be "plyr"). This is to prevent clashes if you're using your own SVG sprite but with the default controls. Most people can ignore this option.
blankVideoStringhttps://cdn.plyr.io/static/blank.mp4Specify a URL or path to a blank video file used to properly cancel network requests.
autoplayBooleanfalseAutoplay the media on load. This is generally advised against on UX grounds. It is also disabled by default in some browsers. If the autoplay attribute is present on a <video> or <audio> element, this will be automatically set to true.
autopause¹BooleantrueOnly allow one player playing at once.
seekTimeNumber10The time, in seconds, to seek when a user hits fast forward or rewind.
volumeNumber1A number, between 0 and 1, representing the initial volume of the player.
mutedBooleanfalseWhether to start playback muted. If the muted attribute is present on a <video> or <audio> element, this will be automatically set to true.
clickToPlayBooleantrueClick (or tap) of the video container will toggle play/pause.
disableContextMenuBooleantrueDisable right click menu on video to help as very primitive obfuscation to prevent downloads of content.
hideControlsBooleantrueHide video controls automatically after 2s of no mouse or focus movement, on control element blur (tab out), on playback start or entering fullscreen. As soon as the mouse is moved, a control element is focused or playback is paused, the controls reappear instantly.
resetOnEndBooleanfalseReset the playback to the start once playback is complete.
keyboardObject{ focused: true, global: false }Enable keyboard shortcuts for focused players only or globally
tooltipsObject{ controls: false, seek: true }controls: Display control labels as tooltips on :hover & :focus (by default, the labels are screen reader only). seek: Display a seek tooltip to indicate on click where the media would seek to.
durationNumbernullSpecify a custom duration for media.
displayDurationBooleantrueDisplays the duration of the media on the "metadataloaded" event (on startup) in the current time display. This will only work if the preload attribute is not set to none (or is not set at all) and you choose not to display the duration (see controls option).
invertTimeBooleantrueDisplay the current time as a countdown rather than an incremental counter.
toggleInvertBooleantrueAllow users to click to toggle the above.
listenersObjectnullAllows binding of event listeners to the controls before the default handlers. See the defaults.js for available listeners. If your handler prevents default on the event (event.preventDefault()), the default handler will not fire.
captionsObject{ active: false, language: 'auto', update: false }active: Toggles if captions should be active by default. language: Sets the default language to load (if available). 'auto' uses the browser language. update: Listen to changes to tracks and update menu. This is needed for some streaming libraries, but can result in unselectable language options).
fullscreenObject{ enabled: true, fallback: true, iosNative: false }enabled: Toggles whether fullscreen should be enabled. fallback: Allow fallback to a full-window solution. iosNative: whether to use native iOS fullscreen when entering fullscreen (no custom controls)
ratioString16:9The aspect ratio you want to use for embedded players.
storageObject{ enabled: true, key: 'plyr' }enabled: Allow use of local storage to store user settings. key: The key name to use.
speedObject{ selected: 1, options: [0.5, 0.75, 1, 1.25, 1.5, 1.75, 2] }selected: The default speed for playback. options: Options to display in the menu. Most browsers will refuse to play slower than 0.5.
qualityObject{ default: 'default', options: ['hd2160', 'hd1440', 'hd1080', 'hd720', 'large', 'medium', 'small', 'tiny', 'default'] }Currently only supported by YouTube. default is the default quality level, determined by YouTube. options are the options to display.
loopObject{ active: false }active: Whether to loop the current video. If the loop attribute is present on a <video> or <audio> element, this will be automatically set to true This is an object to support future functionality.
adsObject{ enabled: false, publisherId: '' }enabled: Whether to enable vi.ai ads. publisherId: Your unique vi.ai publisher ID.
  1. Vimeo only

API

There are methods, setters and getters on a Plyr object.

Object

The easiest way to access the Plyr object is to set the return value from your call to the constructor to a variable. For example:

const player = new Plyr('#player', {
    /* options */
});

You can also access the object through any events:

element.addEventListener('ready', event => {
    const player = event.detail.plyr;
});

Methods

Example method use:

player.play(); // Start playback
player.fullscreen.enter(); // Enter fullscreen
MethodParametersDescription
play()¹-Start playback.
pause()-Pause playback.
togglePlay(toggle)BooleanToggle playback, if no parameters are passed, it will toggle based on current status.
stop()-Stop playback and reset to start.
restart()-Restart playback.
rewind(seekTime)NumberRewind playback by the specified seek time. If no parameter is passed, the default seek time will be used.
forward(seekTime)NumberFast forward by the specified seek time. If no parameter is passed, the default seek time will be used.
increaseVolume(step)NumberIncrease volume by the specified step. If no parameter is passed, the default step will be used.
decreaseVolume(step)NumberIncrease volume by the specified step. If no parameter is passed, the default step will be used.
toggleCaptions(toggle)BooleanToggle captions display. If no parameter is passed, it will toggle based on current status.
fullscreen.enter()-Enter fullscreen. If fullscreen is not supported, a fallback "full window/viewport" is used instead.
fullscreen.exit()-Exit fullscreen.
fullscreen.toggle()-Toggle fullscreen.
airplay()-Trigger the airplay dialog on supported devices.
toggleControls(toggle)BooleanToggle the controls (video only). Takes optional truthy value to force it on/off.
on(event, function)String, FunctionAdd an event listener for the specified event.
once(event, function)String, FunctionAdd an event listener for the specified event once.
off(event, function)String, FunctionRemove an event listener for the specified event.
supports(type)StringCheck support for a mime type.
destroy()-Destroy the instance and garbage collect any elements.
  1. For HTML5 players, play() will return a Promise in some browsers - WebKit and Mozilla according to MDN at time of writing.

Getters and Setters

Example setters:

player.volume = 0.5; // Sets volume at 50%
player.currentTime = 10; // Seeks to 10 seconds

Example getters:

player.volume; // 0.5;
player.currentTime; // 10
player.fullscreen.active; // false;
PropertyGetterSetterDescription
isHTML5-Returns a boolean indicating if the current player is HTML5.
isEmbed-Returns a boolean indicating if the current player is an embedded player.
playing-Returns a boolean indicating if the current player is playing.
paused-Returns a boolean indicating if the current player is paused.
stopped-Returns a boolean indicating if the current player is stopped.
ended-Returns a boolean indicating if the current player has finished playback.
buffered-Returns a float between 0 and 1 indicating how much of the media is buffered
currentTimeGets or sets the currentTime for the player. The setter accepts a float in seconds.
seeking-Returns a boolean indicating if the current player is seeking.
duration-Returns the duration for the current media.
volumeGets or sets the volume for the player. The setter accepts a float between 0 and 1.
mutedGets or sets the muted state of the player. The setter accepts a boolean.
hasAudio-Returns a boolean indicating if the current media has an audio track.
speedGets or sets the speed for the player. The setter accepts a value in the options specified in your config. Generally the minimum should be 0.5.
quality¹Gets or sets the quality for the player. The setter accepts a value from the options specified in your config.
loopGets or sets the current loop state of the player. The setter accepts a boolean.
sourceGets or sets the current source for the player. The setter accepts an object. See source setter below for examples.
posterGets or sets the current poster image for the player. The setter accepts a string; the URL for the updated poster image.
autoplayGets or sets the autoplay state of the player. The setter accepts a boolean.
currentTrackGets or sets the caption track by index. -1 means the track is missing or captions is not active
languageGets or sets the preferred captions language for the player. The setter accepts an ISO two-letter language code. Support for the languages is dependent on the captions you include. If your captions don't have any language data, or if you have multiple tracks with the same language, you may want to use currentTrack instead.
fullscreen.active-Returns a boolean indicating if the current player is in fullscreen mode.
fullscreen.enabled-Returns a boolean indicating if the current player has fullscreen enabled.
pipGets or sets the picture-in-picture state of the player. The setter accepts a boolean. This currently only supported on Safari 10+ on MacOS Sierra+ and iOS 10+.
  1. YouTube only. HTML5 will follow.
  2. HTML5 only

The .source setter

This allows changing the player source and type on the fly.

Video example:

player.source = {
    type: 'video',
    title: 'Example title',
    sources: [
        {
            src: '/path/to/movie.mp4',
            type: 'video/mp4',
            size: 720,
        },
        {
            src: '/path/to/movie.webm',
            type: 'video/webm',
            size: 1080,
        },
    ],
    poster: '/path/to/poster.jpg',
    tracks: [
        {
            kind: 'captions',
            label: 'English',
            srclang: 'en',
            src: '/path/to/captions.en.vtt',
            default: true,
        },
        {
            kind: 'captions',
            label: 'French',
            srclang: 'fr',
            src: '/path/to/captions.fr.vtt',
        },
    ],
};

Audio example:

player.source = {
    type: 'audio',
    title: 'Example title',
    sources: [
        {
            src: '/path/to/audio.mp3',
            type: 'audio/mp3',
        },
        {
            src: '/path/to/audio.ogg',
            type: 'audio/ogg',
        },
    ],
};

YouTube example:

player.source = {
    type: 'video',
    sources: [
        {
            src: 'bTqVqk7FSmY',
            provider: 'youtube',
        },
    ],
};

Note: src can be the video ID or URL

Vimeo example

player.source = {
    type: 'video',
    sources: [
        {
            src: '143418951',
            provider: 'vimeo',
        },
    ],
};

Note: src property for YouTube and Vimeo can either be the video ID or the whole URL.

PropertyTypeDescription
typeStringEither video or audio. Note: YouTube and Vimeo are currently not supported as audio sources.
titleStringOptional. Title of the new media. Used for the aria-label attribute on the play button, and outer container. YouTube and Vimeo are populated automatically.
sourcesArrayThis is an array of sources. For HTML5 media, the properties of this object are mapped directly to HTML attributes so more can be added to the object if required.
poster¹StringThe URL for the poster image (HTML5 video only).
tracks¹StringAn array of track objects. Each element in the array is mapped directly to a track element and any keys mapped directly to HTML attributes so as in the example above, it will render as <track kind="captions" label="English" srclang="en" src="https://cdn.selz.com/plyr/1.0/example_captions_en.vtt" default> and similar for the French version. Booleans are converted to HTML5 value-less attributes.
  1. HTML5 only

Events

You can listen for events on the target element you setup Plyr on (see example under the table). Some events only apply to HTML5 audio and video. Using your reference to the instance, you can use the on() API method or addEventListener(). Access to the API can be obtained this way through the event.detail.plyr property. Here's an example:

player.on('ready', event => {
    const instance = event.detail.plyr;
});

Standard Media Events

Event TypeDescription
progressSent periodically to inform interested parties of progress downloading the media. Information about the current amount of the media that has been downloaded is available in the media element's buffered attribute.
playingSent when the media begins to play (either for the first time, after having been paused, or after ending and then restarting).
playSent when playback of the media starts after having been paused; that is, when playback is resumed after a prior pause event.
pauseSent when playback is paused.
timeupdateThe time indicated by the element's currentTime attribute has changed.
volumechangeSent when the audio volume changes (both when the volume is set and when the muted state is changed).
seekingSent when a seek operation begins.
seekedSent when a seek operation completes.
ratechangeSent when the playback speed changes.
endedSent when playback completes. Note: This does not fire if autoplay is true.
enterfullscreenSent when the player enters fullscreen mode (either the proper fullscreen or full-window fallback for older browsers).
exitfullscreenSent when the player exits fullscreen mode.
captionsenabledSent when captions are enabled.
captionsdisabledSent when captions are disabled.
languagechangeSent when the caption language is changed.
controlshiddenSent when the controls are hidden.
controlsshownSent when the controls are shown.
readyTriggered when the instance is ready for API calls.

HTML5 only

Event TypeDescription
loadstartSent when loading of the media begins.
loadeddataThe first frame of the media has finished loading.
loadedmetadataThe media's metadata has finished loading; all attributes now contain as much useful information as they're going to.
qualitychangeThe quality of playback has changed.
canplaySent when enough data is available that the media can be played, at least for a couple of frames. This corresponds to the HAVE_ENOUGH_DATA readyState.
canplaythroughSent when the ready state changes to CAN_PLAY_THROUGH, indicating that the entire media can be played without interruption, assuming the download rate remains at least at the current level. Note: Manually setting the currentTime will eventually fire a canplaythrough event in firefox. Other browsers might not fire this event.
stalledSent when the user agent is trying to fetch media data, but data is unexpectedly not forthcoming.
waitingSent when the requested operation (such as playback) is delayed pending the completion of another operation (such as a seek).
emptiedhe media has become empty; for example, this event is sent if the media has already been loaded (or partially loaded), and the load() method is called to reload it.
cuechangeSent when a TextTrack has changed the currently displaying cues.
errorSent when an error occurs. The element's error attribute contains more information.

YouTube only

Event TypeDescription
statechangeThe state of the player has changed. The code can be accessed via event.detail.code. Possible values are -1: Unstarted, 0: Ended, 1: Playing, 2: Paused, 3: Buffering, 5: Video cued. See the YouTube Docs for more information.

Note: These events also bubble up the DOM. The event target will be the container element.

Some event details borrowed from MDN.

Embeds

YouTube and Vimeo are currently supported and function much like a HTML5 video. Similar events and API methods are available for all types. However if you wish to access the API's directly. You can do so via the embed property of your player object - e.g. player.embed. You can then use the relevant methods from the third party APIs. More info on the respective API's here:

Note: Not all API methods may work 100%. Your mileage may vary. It's better to use the Plyr API where possible.

Shortcuts

By default, a player will bind the following keyboard shortcuts when it has focus. If you have the global option to true and there's only one player in the document then the shortcuts will work when any element has focus, apart from an element that requires input.

KeyAction
0 to 9Seek from 0 to 90% respectively
spaceToggle playback
KToggle playback
Seek backward by the seekTime option
Seek forward by the seekTime option
Increase volume
Decrease volume
MToggle mute
FToggle fullscreen
CToggle captions
LToggle loop

Fullscreen

Fullscreen in Plyr is supported by all browsers that currently support it.

Browser support

Plyr supports the last 2 versions of most modern browsers.

BrowserSupported
Safari
Mobile Safari¹
Firefox
Chrome
Opera
Edge
IE11³
IE10²³
  1. Mobile Safari on the iPhone forces the native player for <video> unless the playsinline attribute is present. Volume controls are also disabled as they are handled device wide.
  2. Native player used (no support for <progress> or <input type="range">) but the A