2.0.20 • Published 7 years ago

plyr-extended v2.0.20

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

This branch is not production ready. There will be bugs.

Plyr

A simple, accessible and customizable HTML5, YouTube and Vimeo media player.

Donate to support Plyr

Checkout the demo

Image of Plyr

Why?

We wanted a lightweight, accessible and customizable media player that supports modern browsers. Sure, there are many other players out there but we wanted to keep things simple, using the right elements for the job.

Features

  • Accessible - full support for VTT captions and screen readers
  • Lightweight - under 10KB minified and gzipped
  • Customisable - make the player look how you want with the markup you want
  • Semantic - 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
  • Streaming - support for hls.js, Shaka and dash.js streaming playback
  • API - toggle playback, volume, seeking, and more
  • 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
  • i18n support - support for internationalization of controls
  • No dependencies - written in "vanilla" JavaScript, no jQuery required
  • SASS and LESS - 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.

Features currently being developed

  • Playback speed selection
  • Quality selection
  • Caption language selection
  • AirPlay
  • Picture in Picture (MacOS Sierra + Safari)

more info

Planned features

  • Playlists
  • Google cast
  • Facebook video support
  • Wistia video support
  • YouTube and Vimeo audio support
  • Audio captions ...and whatever else has been raised in issues

If you have any cool ideas or features, please let me know by creating an issue or, of course, forking and sending a pull request.

CMS plugins

WordPress

Created and maintained by Ryan Anthony Drake (@iamryandrake)

Neos

Created and maintained by Jon Uhlmann (@jonnitto)

Kirby

Created and maintained by Dominik Pschenitschni (@dpschen)

Using package managers

You can grab the source using one of the following package managers.

npm

npm install plyr

https://www.npmjs.com/package/plyr

Bower

bower install plyr

http://bower.io/search/?q=plyr

More info on setting up dependencies can be found in the Bower Docs

Ember

The awesome @louisrudner has created an ember component, available by running:

ember addon:install ember-cli-plyr

More info is on npm and GitHub

Quick setup

Here's a quick run through on getting up and running. There's also a demo on Codepen.

HTML

Plyr extends upon the standard HTML5 markup so that's all you need for those types. More info on advanced HTML markup can be found under initialising.

HTML5 Video

<video poster="/path/to/poster.jpg" 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 controls>
  <source src="/path/to/audio.mp3" type="audio/mp3">
  <source src="/path/to/audio.ogg" type="audio/ogg">
</audio>

For YouTube and Vimeo, Plyr uses the standard YouTube API markup (an empty <div>):

YouTube embed

<div data-type="youtube" data-video-id="bTqVqk7FSmY"></div>

Note: data-video-id value can now be the ID or URL for the video. This attribute name will change in a future release to reflect this change.

Vimeo embed

<div data-type="vimeo" data-video-id="143418951"></div>

Note: data-video-id value can now be the ID or URL for the video. This attribute name will change in a future release to reflect this change.

JavaScript

Include the plyr.js script before the closing </body> tag and then call plyr.setup(). More info on setup() can be found under initialising.

<script src="path/to/plyr.js"></script>
<script>plyr.setup();</script>

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

<script src="https://cdn.plyr.io/2.0.13/plyr.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/2.0.13/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/2.0.13/plyr.svg.

Advanced

LESS & SASS/SCSS

You can use plyr.less or plyr.scss file included in /src as part of your build and change variables to suit your design. The LESS and SASS require you to use the autoprefixer plugin (you should already) as all declerations use the W3C definitions - e.g. appearance: none; will be prefixed to -webkit-appearance: none; by autoprefixer.

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

By default, setup() will find all <video>, <audio> and [data-type] elements with the document and initialises on any found. Each target media element found will be wrapped in a <div> for styling and setup individually. You can specify a variety of arguments to setup() to use, including a different NodeList, HTMLElement, Array of HTMLElements or string selector as below:

Passing a NodeList:

plyr.setup(document.querySelectorAll('.js-player'), options);

Passing a HTMLElement:

plyr.setup(document.querySelector('.js-player'), options);

Passing an Array of HTMLElements:

plyr.setup([
	document.querySelector('.js-player-1'),
	document.querySelector('.js-player-2')
], options);

Passing a string selector:

plyr.setup('.js-player', options);

The NodeList, HTMLElement or string selector can be the target <video>, <audio> or [data-type] (for embeds) element itself or a container element.

Passing just the options object:

plyr.setup(options);

setup() will return an array of instances that can be used with the API methods. See the API section for more info.

RangeTouch

Some touch browsers (particularly Mobile Safari on iOS) seem to have issues with <input type="range"> elements whereby touching the track to set the value doesn't work and sliding the thumb can be tricky. To combat this, I've created RangeTouch which I'd recommend including in your solution. It's a tiny script with a nice benefit for users on touch devices.

Options

Options must be passed as an object to the setup() method as above or as JSON in data-plyr attribute on each of your target elements:

<video data-plyr='{ title: "testing" }'></video>

Note the single quotes encapsulating the JSON and double quotes on the object keys.

Fullscreen options

API

Instance

The easiest way to access the plyr instances is to store the return value from your call to setup():

var players = plyr.setup('.js-player');

This will return an array of all instances that were setup. Another way is to use plyr.get() to get all instances within a given container, for example:

var players = plyr.get('.js-player');

If no argument is passed, it will find all instances in the current document. This will return an array of all instances that were found in the given selector.

A final option is to access the instance through the event handlers:

instance.on('ready', function(event) {
  var instance = event.detail.plyr;
});

Methods

Once you have your instances, you can use the API methods below on it. For example to pause the first player:

players[0].pause();

Here's a list of the methods supported:

.source() method

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

Video example:

player.source({
  type:       'video',
  title:      'Example title',
  sources: [{
      src:    '/path/to/movie.mp4',
      type:   'video/mp4'
  },
  {
      src:    '/path/to/movie.webm',
      type:   'video/webm'
  }],
  poster:     '/path/to/poster.jpg',
  tracks:     [{
      kind:   'captions',
      label:  'English',
      srclang:'en',
      src:    '/path/to/captions.vtt',
      default: true
  }],
  loopKeyEvents: {
      toggleLoop: 76,
      loopin:     73,
      loopout:    79
  }
});

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',
  title:      'Example title',
  sources: [{
      src:    'bTqVqk7FSmY',
      type:   'youtube'
  }]
});

Note: src can be the video ID or URL

Vimeo example

player.source({
  type:       'video',
  title:      'Example title',
  sources: [{
      src:    '143418951',
      type:   'vimeo'
  }]
});

Note: src can be the video ID or URL

More details on the object parameters

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:

instance.on('ready', function(event) {
  var instance = event.detail.plyr;
});

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

Details borrowed from: https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Media_events

Embeds

YouTube and Vimeo are currently supported and function much like a HTML5 video. Check the relevant documentation sections for any differences.

Plyr references a custom version of the Vimeo Froogaloop API as Vimeo have neglected to maintain the library and there were bugs with their version. You don't need to worry about including your own versions of the Vimeo or YouTube JavaScript APIs.

The embed third party API's can be accessed through the getEmbed() API method.

More info on the respective API's here:

Please note: not all API methods may work 100%. Your mileage may vary. It's better to use the universal 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.

Streaming

Because Plyr is an extension of the standard HTML5 video and audio elements, third party streaming plugins can be used with Plyr. Massive thanks to Matias Russitto (@russitto) for working on this. Here's a few examples:

Fullscreen

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

Browser support

¹ Mobile Safari on the iPhone forces the native player for <video> so no useful customization is possible. <audio> elements have volume controls disabled.

² Native player used (no support for <progress> or <input type="range">) but the API is supported (v1.0.28+)

³ IE10 has no native fullscreen support, fallback can be used (see options)

The enabled option can be used to disable certain User Agents. For example, if you don't want to use Plyr for smartphones, you could use:

enabled: /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent)

If a User Agent is disabled but supports <video> and <audio> natively, it will use the native player.

Any unsupported browsers will display links to download the media if the correct html is used.

Issues

If you find anything weird with Plyr, please let us know using the GitHub issues tracker.

Author

Plyr is developed by @sam_potts / sampotts.me with help from the awesome contributors

Donate

Plyr costs money to run, not my time - I donate that for free but domains, hosting and more. Any help is appreciated... Donate to support Plyr

Mentions

Used by

Let me know on Twitter I can add you to the above list. It'd be awesome to see how you're using Plyr :-)

Useful links and credits

Credit to the PayPal HTML5 Video player from which Plyr's caption functionality is ported from:

Also these links helped created Plyr:

Thanks

Fastly

Thanks to Fastly for providing the CDN services.

Copyright and License

The MIT license.