diff options
Diffstat (limited to 'readme.md')
-rw-r--r-- | readme.md | 403 |
1 files changed, 247 insertions, 156 deletions
@@ -30,8 +30,8 @@ Oh and yes, it works with Bootstrap. Check out the [changelog](changelog.md) to see what's new with Plyr. ## Planned Development -- Streaming - Playback speed +- Quality selection - Playlists - Multiple language captions (with selection) - Audio captions @@ -187,8 +187,8 @@ plyr.setup(document.querySelector('.js-player'), options); Passing an [Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) of [HTMLElement](https://developer.mozilla.org/en/docs/Web/API/HTMLElement)s: ```javascript plyr.setup([ - document.querySelector('.js-player'), - document.querySelector('.another-js-player') + document.querySelector('.js-player-1'), + document.querySelector('.js-player-2') ], options); ``` @@ -204,7 +204,7 @@ Passing just the options object: plyr.setup(options); ``` -`setup()` will return an array of all the elements Plyr was setup on. The `plyr` object can be accessed on these elements and used for the API. +`setup()` will return an array of instances that can be used with the [#API](API) methods. See the [#API](API) section for more. #### 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](https://rangetouch.com) which I'd recommend including in your solution. It's a tiny script with a nice benefit for users on touch devices. @@ -319,6 +319,12 @@ Note the single quotes encapsulating the JSON and double quotes on the object ke <td>This will restore and *reload* HTML5 video once playback is complete. Note: depending on the browser caching, this may result in the video downloading again (or parts of it). Use with caution.</td> </tr> <tr> + <td><code>keyboardShortcuts</code></td> + <td>Boolean</td> + <td><code>true</code></td> + <td>Enable <a href="#shortcuts">keyboard shortcuts</a></td> + </tr> + <tr> <td><code>tooltips</code></td> <td>Object</td> <td><code>{ controls: false, seek: true }</code></td> @@ -414,29 +420,36 @@ Note the single quotes encapsulating the JSON and double quotes on the object ke ## API -#### Getting the `plyr` instance -A `plyr` object is added to any element that Plyr is initialized on. You can then control the player by accessing methods in the `plyr` object. +### Instance + +The easiest way to access the plyr instances is to store the return value from your call to `setup()`: -There are two ways to access the instance, firstly you re-query the element container you used for setup (e.g. `.js-player`) like so: +```javascript +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: ```javascript -var player = document.querySelector('.js-player').plyr; +var players = plyr.get('.js-player'); ``` -You can listen for the `setup` [event](#events) on the container, after which the `plyr` key will be available and also passed in the to your callback (in the `plyr` key of the event object). +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. -The other method is using the return value from the call to `setup()`. An array of instances is returned so you need to use an index: +A final option is to access the instance through the event handlers: ```javascript -var player = plyr.setup('.js-player')[0].plyr; +instance.on('ready', function(event) { + var instance = event.detail.plyr; +}); ``` -This will return an array of plyr instances that were setup, so you need to specify the index of the instance you want or loop through of course. This is less useful if you are setting up multiple instances. +### Methods -Once you have your instance, you can use the API methods below on it. For example to pause it: +Once you have your instances, you can use the API methods below on it. For example to pause the first player: ```javascript -player.pause(); +players[0].pause(); ``` Here's a list of the methods supported: @@ -451,6 +464,36 @@ Here's a list of the methods supported: </thead> <tbody> <tr> + <td><code>getContainer()</code></td> + <td>—</td> + <td>Get the players outer container element that is automatically injected.</td> + </tr> + <tr> + <td><code>getMedia()</code></td> + <td>—</td> + <td>Get the media element (<code>>video<</code>, <code>>audio<</code> or <code>>div<</code> for YouTube or Vimeo).</td> + </tr> + <tr> + <td><code>getEmbed()</code></td> + <td>—</td> + <td>Get the embed API to access those methods - either YouTube or Vimeo.</td> + </tr> + <tr> + <td><code>getType()</code></td> + <td>—</td> + <td>Get the type - 'video', 'audio', 'youtube' or 'vimeo'.</td> + </tr> + <tr> + <td><code>isReady()</code></td> + <td>—</td> + <td>Determine if the player is ready to accept API calls - this is because HTML5 is ready instantly but YouTube and Vimeo can take some time to load their APIs.</td> + </tr> + <tr> + <td><code>on()</code></td> + <td>String, Function</td> + <td>Watch for an event (first argument) and run a callback function (second argument). This saves you doing your own <code>addEventListner</code>s.</td> + </tr> + <tr> <td><code>play()</code></td> <td>—</td> <td>Plays the media</td> @@ -460,7 +503,12 @@ Here's a list of the methods supported: <td>—</td> <td>Pauses the media</td> </tr> - <tr> + <tr> + <td><code>stop()</code></td> + <td>—</td> + <td>Stops the media</td> + </tr> + <tr> <td><code>restart()</code></td> <td>—</td> <td>Restarts playback</td> @@ -660,171 +708,214 @@ Some more details on the object parameters </table> ## 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. +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: + +```javascript +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. <table class="table" width="100%"> <thead> <tr> <th width="20%">Event name</th> - <th width="20%">HTML5 only</th> + <th width="20%">HTML5 only</th> <th width="60%">Description</th> </tr> </thead> <tbody> - <tr> - <td><code>canplay</code></td> - <td>✔</td> - <td>Sent when enough data is available that the media can be played, at least for a couple of frames. This corresponds to the <code>HAVE_ENOUGH_DATA</code> <code>readyState</code>.</td> - </tr> - <tr> - <td><code>canplaythrough</code></td> - <td></td> - <td>Sent when the ready state changes to <code>CAN_PLAY_THROUGH</code>, indicating that the entire media can be played without interruption, assuming the download rate remains at least at the current level. <strong>Note</strong>: Manually setting the <code>currentTime</code> will eventually fire a <code>canplaythrough</code> event in firefox. Other browsers might not fire this event.</td> - </tr> - <tr> - <td><code>emptied</code></td> - <td>✔</td> - <td>The media has become empty; for example, this event is sent if the media has already been loaded (or partially loaded), and the <code>load()</code> method is called to reload it.</td> - </tr> - <tr> - <td><code>ended</code></td> - <td></td> - <td>Sent when playback completes.</td> - </tr> - <tr> - <td><code>error</code></td> - <td>✔</td> - <td>Sent when an error occurs. The element's <code>error</code> attribute contains more information.</td> - </tr> - <tr> - <td><code>loadeddata</code></td> - <td>✔</td> - <td>The first frame of the media has finished loading.</td> - </tr> - <tr> - <td><code>loadedmetadata</code></td> - <td>✔</td> - <td>The media's metadata has finished loading; all attributes now contain as much useful information as they're going to.</td> - </tr> - <tr> - <td><code>loadstart</code></td> - <td>✔</td> - <td>Sent when loading of the media begins.</td> - </tr> - <tr> - <td><code>pause</code></td> - <td></td> - <td>Sent when playback is paused.</td> - </tr> - <tr> - <td><code>play</code></td> - <td></td> - <td>Sent when playback of the media starts after having been paused; that is, when playback is resumed after a prior <code>pause</code> event.</td> - </tr> - <tr> - <td><code>playing</code></td> - <td></td> - <td>Sent when the media begins to play (either for the first time, after having been paused, or after ending and then restarting).</td> - </tr> - <tr> - <td><code>progress</code></td> - <td></td> - <td>Sent 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 <code>buffered</code> attribute.</td> - </tr> - <tr> - <td><code>seeked</code></td> - <td>✔</td> - <td>Sent when a seek operation completes.</td> - </tr> - <tr> - <td><code>seeking</code></td> - <td>✔</td> - <td>Sent when a seek operation begins.</td> - </tr> - <tr> - <td><code>stalled</code></td> - <td>✔</td> - <td>Sent when the user agent is trying to fetch media data, but data is unexpectedly not forthcoming.</td> - </tr> - <tr> - <td><code>timeupdate</code></td> - <td></td> - <td>The time indicated by the element's <code>currentTime</code> attribute has changed.</td> - </tr> - <tr> - <td><code>volumechange</code></td> - <td></td> - <td>Sent when the audio volume changes (both when the volume is set and when the <code>muted</code> attribute is changed).</td> - </tr> - <tr> - <td><code>waiting</code></td> - <td>✔</td> - <td>Sent when the requested operation (such as playback) is delayed pending the completion of another operation (such as a seek).</td> - </tr> - <tr> - <td><code>enterfullscreen</code></td> - <td></td> - <td>User enters fullscreen (either the proper fullscreen or full-window fallback for older browsers)</td> - </tr> - <tr> - <td><code>exitfullscreen</code></td> - <td></td> - <td>User exits fullscreen</td> - </tr> - <tr> - <td><code>captionsenabled</code></td> - <td></td> - <td>Captions toggled on</td> - </tr> - <tr> - <td><code>captionsdisabled</code></td> - <td></td> - <td>Captions toggled off</td> - </tr> - <tr> - <td><code>ready</code></td> - <td></td> - <td>Triggered when initial setup is done or a source change has occurred.</td> - </tr> + <tr> + <td><code>setup</code></td> + <td></td> + <td>When an initial setup has completed</td> + </tr> + <tr> + <td><code>ready</code></td> + <td></td> + <td>Triggered when the instance is ready for API use and external APIs are ready (YouTube and Vimeo).</td> + </tr> + <tr> + <td><code>canplay</code></td> + <td>✔</td> + <td>Sent when enough data is available that the media can be played, at least for a couple of frames. This corresponds to the <code>HAVE_ENOUGH_DATA</code> <code>readyState</code>.</td> + </tr> + <tr> + <td><code>canplaythrough</code></td> + <td></td> + <td>Sent when the ready state changes to <code>CAN_PLAY_THROUGH</code>, indicating that the entire media can be played without interruption, assuming the download rate remains at least at the current level. <strong>Note</strong>: Manually setting the <code>currentTime</code> will eventually fire a <code>canplaythrough</code> event in firefox. Other browsers might not fire this event.</td> + </tr> + <tr> + <td><code>emptied</code></td> + <td>✔</td> + <td>The media has become empty; for example, this event is sent if the media has already been loaded (or partially loaded), and the <code>load()</code> method is called to reload it.</td> + </tr> + <tr> + <td><code>ended</code></td> + <td></td> + <td>Sent when playback completes.</td> + </tr> + <tr> + <td><code>error</code></td> + <td>✔</td> + <td>Sent when an error occurs. The element's <code>error</code> attribute contains more information.</td> + </tr> + <tr> + <td><code>loadeddata</code></td> + <td>✔</td> + <td>The first frame of the media has finished loading.</td> + </tr> + <tr> + <td><code>loadedmetadata</code></td> + <td>✔</td> + <td>The media's metadata has finished loading; all attributes now contain as much useful information as they're going to.</td> + </tr> + <tr> + <td><code>loadstart</code></td> + <td>✔</td> + <td>Sent when loading of the media begins.</td> + </tr> + <tr> + <td><code>pause</code></td> + <td></td> + <td>Sent when playback is paused.</td> + </tr> + <tr> + <td><code>play</code></td> + <td></td> + <td>Sent when playback of the media starts after having been paused; that is, when playback is resumed after a prior <code>pause</code> event.</td> + </tr> + <tr> + <td><code>playing</code></td> + <td></td> + <td>Sent when the media begins to play (either for the first time, after having been paused, or after ending and then restarting).</td> + </tr> + <tr> + <td><code>progress</code></td> + <td></td> + <td>Sent 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 <code>buffered</code> attribute.</td> + </tr> + <tr> + <td><code>seeked</code></td> + <td>✔</td> + <td>Sent when a seek operation completes.</td> + </tr> + <tr> + <td><code>seeking</code></td> + <td>✔</td> + <td>Sent when a seek operation begins.</td> + </tr> + <tr> + <td><code>stalled</code></td> + <td>✔</td> + <td>Sent when the user agent is trying to fetch media data, but data is unexpectedly not forthcoming.</td> + </tr> + <tr> + <td><code>timeupdate</code></td> + <td></td> + <td>The time indicated by the element's <code>currentTime</code> attribute has changed.</td> + </tr> + <tr> + <td><code>volumechange</code></td> + <td></td> + <td>Sent when the audio volume changes (both when the volume is set and when the <code>muted</code> attribute is changed).</td> + </tr> + <tr> + <td><code>waiting</code></td> + <td>✔</td> + <td>Sent when the requested operation (such as playback) is delayed pending the completion of another operation (such as a seek).</td> + </tr> + <tr> + <td><code>enterfullscreen</code></td> + <td></td> + <td>User enters fullscreen (either the proper fullscreen or full-window fallback for older browsers)</td> + </tr> + <tr> + <td><code>exitfullscreen</code></td> + <td></td> + <td>User exits fullscreen</td> + </tr> + <tr> + <td><code>captionsenabled</code></td> + <td></td> + <td>Captions toggled on</td> + </tr> + <tr> + <td><code>captionsdisabled</code></td> + <td></td> + <td>Captions toggled off</td> + </tr> + <tr> + <td><code>destroyed</code></td> + <td></td> + <td>When an instance is destroyed. The original element that replaced the container will be the event target.</td> + </tr> </tbody> </table> Details borrowed from: [https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Media_events](https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Media_events) -Here's an example of binding an event listener: - -```javascript -document.querySelector('.js-plyr').addEventListener('ready', function(event) { - var player = event.target.plyr; -}); -``` - -These events also bubble up the DOM. - ## 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 native API's can be accessed through the `embed` property of the plyr object. For example: +The embed third party API's can be accessed through the `getEmbed()` API method. -```javascript -document.querySelector('.js-plyr').addEventListener('ready', function(event) { - var player = event.target.plyr; +More info on the respective API's here: - // YouTube - console.log(player.embed.getVideoData()); +- [YouTube API Reference](https://developers.google.com/youtube/js_api_reference) +- [Vimeo API Reference](https://developer.vimeo.com/player/js-api#reference) - // Vimeo - console.log(player.embed.api('getColor')); -}); -``` +*Please note*: not all API methods may work 100%. Your mileage may vary. It's better to use the universal plyr API where possible. -More info on the respective API's here: -[YouTube API Reference](https://developers.google.com/youtube/js_api_reference) -[Vimeo API Reference](https://developer.vimeo.com/player/js-api#reference) +## Shortcuts +By default, a focused player will bind the following keyboard shortcuts: -*Please note*: not all API methods may work 100%. Your mileage may vary. It's better to use the universal plyr API where possible. +<table class="table" width="100%"> + <thead> + <tr> + <th width="25%">Key</th> + <th width="75%">Action</th> + </tr> + </thead> + <tbody> + <tr> + <td><code>0</code> to <code>9</code></td> + <td>Seek from 0 to 90% respectively</td> + </tr> + <tr> + <td><code>space</code> or <code>K</code></td> + <td>Toggle playback</td> + </tr> + <tr> + <td><code>→</code></td> + <td>Seek forward by the <code>seekTime</code> option</td> + </tr> + <tr> + <td><code>↑</code></td> + <td>Increase volume</td> + </tr> + <tr> + <td><code>↓</code></td> + <td>Decrease volume</td> + </tr> + <tr> + <td><code>M</code></td> + <td>Toggle mute</td> + </tr> + <tr> + <td><code>F</code></td> + <td>Toggle fullscreen</td> + </tr> + <tr> + <td><code>C</code></td> + <td>Toggle captions</td> + </tr> + </tbody> +</table> ## 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](https://github.com/russitto)) for working on this. Here's a few examples: |