Git Product home page Git Product logo

plyr's Introduction

Plyr

A simple, accessible HTML5 media player.

Checkout the demo

Image of Plyr

Why?

We wanted a lightweight, accessible and customisable media player that just 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 captions and screen readers.
  • Lightweight - just 6KB 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 - as you'd expect these days.
  • Audio & Video - support for both formats.
  • API - toggle playback, volume, seeking, and more.
  • Fullscreen - supports native fullscreen with fallback to "full window" modes.
  • No dependencies - written in vanilla JavaScript, no jQuery required.

Oh and yes, it works with Bootstrap.

Changelog

Check out the changelog

Planned development

  • Accept a string selector, a node, or a nodelist for the container property of selectors.
  • Multiple language captions (with selection)
  • Playlists (audio and video)
  • Set source by API
  • Tooltip option (for seeking and controls) ... 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.

Implementation

Check docs/index.html and docs/dist/docs.js for an example setup.

Heads up, the example index.html file needs to be served from a webserver (such as Apache, Nginx, IIS or similar) unless you change the file sources to include http or https. e.g. change //cdn.plyr.io/1.0.29/plyr.js to https://cdn.plyr.io/1.0.29/plyr.js

Bower

If bower is your thang, you can grab Plyr using:

bower install plyr

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

CDN

If you want to use our CDN, you can use the following. HTTPS (SSL) is supported.

<link rel="stylesheet" href="//cdn.plyr.io/1.0.29/plyr.css">
<script src="//cdn.plyr.io/1.0.29/plyr.js"></script>

You can also access the sprite.svg file at //cdn.plyr.io/1.0.29/sprite.svg.

CSS

If you want to use the default css, add the plyr.css file from /dist into your head, or even better use plyr.less or plyr.sass file included in /src in your build to save a request.

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

SVG

The SVG sprite for the controls icons is loaded in by AJAX to help with performance. This is best added before the closing </body>, before any other scripts.

<script>
(function(d,p){
	var a=new XMLHttpRequest(),
		b=d.body;
	a.open("GET",p,!0);
	a.send();
	a.onload=function(){
		var c=d.createElement("div");
		c.style.display="none";
		c.innerHTML=a.responseText;
		b.insertBefore(c,b.childNodes[0])
	}
})(document,"dist/sprite.svg");
</script>

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/

HTML

The only extra markup that's needed to use plyr is a <div> wrapper. Replace the source, poster and captions with urls for your media.

<div class="player">
	<video poster="//cdn.selz.com/plyr/1.0/poster.jpg" controls crossorigin>
		<!-- Video files -->
		<source src="//cdn.selz.com/plyr/1.0/movie.mp4" type="video/mp4">
		<source src="//cdn.selz.com/plyr/1.0/movie.webm" type="video/webm">
		
		<!-- Text track file -->
		<track kind="captions" label="English captions" src="//cdn.selz.com/plyr/1.0/movie_captions_en.vtt" srclang="en" default>
		
		<!-- Fallback for browsers that don't support the <video> element -->
		<div>
			<a href="//cdn.selz.com/plyr/1.0/movie.mp4">Download</a>
		</div>
	</video>
</div>

And the same for <audio>

<div class="player">
	<audio controls>
		<!-- Audio files -->
		<source src="//cdn.selz.com/plyr/1.0/logistics-96-sample.mp3" type="audio/mp3">
    <source src="//cdn.selz.com/plyr/1.0/logistics-96-sample.ogg" type="audio/ogg">
		
		<!-- Fallback for browsers that don't support the <audio> element -->
		<div>
			<a href="//cdn.selz.com/plyr/1.0/logistics-96-sample.mp3">Download</a>
		</div>
	</audio>
</div>

Cross Origin (CORS)

You'll notice the crossorigin attribute on the example <video> and <audio> elements. This is because the media is loaded from another domain. If your media is hosted on another domain, you may need to add this attribute.

More info on CORS here: https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

###JavaScript Much of the behaviour of the player is configurable when initialising the library. Below is an example of a default instance.

<script src="dist/plyr.js"></script>
<script>
plyr.setup({
	*options*
});
</script>

Options

You can pass the following options to the setup method.

Option Type Default Description
enabled Boolean true Completely disable Plyr. This would allow you to do a User Agent check or similar to programatically enable or disable Plyr for a certain UA. Example below.
html String See controls.md See controls.md for more info on how the html needs to be structured.
debug Boolean false Display debugging information on what Plyr is doing.
seekTime Number 10 The time, in seconds, to seek when a user hits fast forward or rewind.
volume Number 5 A number, between 1 and 10, representing the initial volume of the player.
click Boolean true Click (or tap) will toggle pause/play of a <video>.
tooltips Boolean false Display control labels as tooltips on :hover & :focus (by default, the labels are screen reader only).
selectors Object See plyr.js in /src for more info. The only option you might want to change is player which is the hook used for Plyr, the default is .player.
classes Object Similar to above, these are the classes added to the player when state changes occur.
captions Object One property defaultActive which toggles if captions should be on by default. The default value is false.
fullscreen Object Three properties; enabled which toggles if fullscreen should be enabled (if the browser supports it). The default value is true. A fallback property which will enable a full window view for older browsers. The default value is true. A hideControls property which will hide the controls when fullscreen is active and the video is playing, after 1s. The controls reappear on hover of the progress bar (mouse), focusing a child control or pausing the video (by tap/click of video if `click` is `true`). The default value is true.
storage Object Two properties; enabled which toggles if local storage should be enabled (if the browser supports it). The default value is `true`. This enables storing user settings, currently it only stores volume but more will be added later. The second property key is the key used for the local storage. The default is plyr_volume until more settings are stored.

API

A plyr object is added to any element that Plyr is initialised on. You can then control the player by accessing methods in the plyr object. For example if you wanted to pause Plyr:

document.querySelectorAll(".player")[0].plyr.pause();

Here's a list of the methods supported:

Method Parameters Description
play() Plays the media
pause() Pauses the media
restart() Restarts playback
rewind(...) Number Rewinds by the provided parameter, in seconds. If no parameter is provided, the default seekInterval is used (10 seconds).
forward(...) Number Fast forwards by the provided parameter, in seconds. If no parameter is provided, the default seekInterval is used (10 seconds).
seek(...) Number Seeks the media to the provided parameter, time in seconds.
setVolume(...) Number Sets the player volume to the provided parameter. The value should be between 0 (muted) and 10 (loudest). If no parameter is provided, the default volume is used (5). Values over 10 are ignored.
toggleMute() Toggles mute for the player.
toggleCaptions() Toggles whether captions are enabled.
support(...) String Determine if a player supports a certain MIME type.
source(...) String or Array Set the media source.

string
.source("/path/to/video.mp4")
This will set the src attribute on the video or audio element.

array
.source([{ src: "/path/to/video.webm", type: "video/webm", ...more attributes... }, { src: "/path/to/video.mp4", type: "video/mp4", ...more attributes... }])`
This will inject a child `source` element for every element in the array with the specified attributes. `src` is the only required attribute although adding `type` is recommended as it helps the browser decide which file to download and play.
poster(...) String Set the poster url. This is supported for the video element only.

Events/Callbacks

The plyr object on the player element also contains a media property which is a reference to the <audio> or <video> element within the player which you can use to listen for events. Here's an example:

var media = document.querySelectorAll(".player")[0].plyr.media;

media.addEventListener("playing", function() { 
  console.log("playing");
});

A complete list of events can be found here: Media Events - W3.org

Fullscreen

Fullscreen in Plyr is supported for all browsers that currently support it. If you're using the default CSS, you can also use a "full browser" mode which will use the full browser window by adding the player-fullscreen class to your container.

Browser support

Safari Firefox Chrome Opera IE9 IE10+
✔¹ API² ✔³

¹ Mobile Safari on the iPhone forces the native player for <video> so no useful customisation 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 (@sam_potts) (sampotts.me)

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:

Copyright and License

The MIT license.

plyr's People

Contributors

brunobatista avatar brunowego avatar pborreli avatar

Watchers

James Cloos avatar Joseph Santana avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.