Work on docs

This commit is contained in:
Sam 2016-08-21 10:30:11 +10:00
parent 92b9e3400b
commit aac0a5a3a9
4 changed files with 300 additions and 172 deletions

View File

@ -20,12 +20,14 @@ And some other changes and bug fixes:
- `on()` to provide an easy way to listen to events
- `stop()` to, you guessed it, stop the player
- `destroy()` now works correctly for YouTube and Vimeo (fixes #272)
- New `destoryed` event when original element is restored (original element passed as event target)
- New `destroyed` event when original element is restored (original element passed as event target)
- Default volume is now 10 (max) rather than 5
- Sprite is only loaded once (fixes #259)
- Fixes for Vimeo post message bugs on source change or destroy (fixes #318)
- Save caption state in storage (fixes #311)
- Added keyboard shortcuts to the current focused player (with option to disable) (fixes #309)
- Fix for captions bug (fixes #332)
- Change to AMD (fixes #298)
## v1.8.12
- Vimeo keyboard focus fix (Fixes #317)

4
dist/plyr.js vendored

File diff suppressed because one or more lines are too long

407
readme.md
View File

@ -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.
@ -318,6 +318,12 @@ Note the single quotes encapsulating the JSON and double quotes on the object ke
<td><code>false</code></td>
<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>
@ -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
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:
The easiest way to access the plyr instances is to store the return value from your call to `setup()`:
```javascript
var player = document.querySelector('.js-player').plyr;
var players = plyr.setup('.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).
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:
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 = plyr.setup('.js-player')[0].plyr;
var players = plyr.get('.js-player');
```
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.
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.
Once you have your instance, you can use the API methods below on it. For example to pause it:
A final option is to access the instance through the event handlers:
```javascript
player.pause();
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:
```javascript
players[0].pause();
```
Here's a list of the methods supported:
@ -450,6 +463,36 @@ Here's a list of the methods supported:
</tr>
</thead>
<tbody>
<tr>
<td><code>getContainer()</code></td>
<td>&mdash;</td>
<td>Get the players outer container element that is automatically injected.</td>
</tr>
<tr>
<td><code>getMedia()</code></td>
<td>&mdash;</td>
<td>Get the media element (<code>&gt;video&lt;</code>, <code>&gt;audio&lt;</code> or <code>&gt;div&lt;</code> for YouTube or Vimeo).</td>
</tr>
<tr>
<td><code>getEmbed()</code></td>
<td>&mdash;</td>
<td>Get the embed API to access those methods - either YouTube or Vimeo.</td>
</tr>
<tr>
<td><code>getType()</code></td>
<td>&mdash;</td>
<td>Get the type - 'video', 'audio', 'youtube' or 'vimeo'.</td>
</tr>
<tr>
<td><code>isReady()</code></td>
<td>&mdash;</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>&mdash;</td>
@ -460,7 +503,12 @@ Here's a list of the methods supported:
<td>&mdash;</td>
<td>Pauses the media</td>
</tr>
<tr>
<tr>
<td><code>stop()</code></td>
<td>&mdash;</td>
<td>Stops the media</td>
</tr>
<tr>
<td><code>restart()</code></td>
<td>&mdash;</td>
<td>Restarts playback</td>
@ -660,172 +708,215 @@ 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.&nbsp; 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.&nbsp; 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:
```javascript
document.querySelector('.js-plyr').addEventListener('ready', function(event) {
var player = event.target.plyr;
// YouTube
console.log(player.embed.getVideoData());
// Vimeo
console.log(player.embed.api('getColor'));
});
```
The embed third party API's can be accessed through the `getEmbed()` API method.
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)
- [YouTube API Reference](https://developers.google.com/youtube/js_api_reference)
- [Vimeo API Reference](https://developer.vimeo.com/player/js-api#reference)
*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 focused player will bind the following keyboard shortcuts:
<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>&rarr;</code></td>
<td>Seek forward by the <code>seekTime</code> option</td>
</tr>
<tr>
<td><code>&uarr;</code></td>
<td>Increase volume</td>
</tr>
<tr>
<td><code>&darr;</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:

View File

@ -489,9 +489,9 @@
}*/
// Trigger event
function _triggerEvent(element, eventName, bubbles, properties) {
function _event(element, type, bubbles, properties) {
// Bail if no element
if (!element || !eventName) {
if (!element || !type) {
return;
}
@ -501,7 +501,7 @@
}
// Create and dispatch the event
var event = new CustomEvent(eventName, {
var event = new CustomEvent(type, {
bubbles: bubbles,
detail: properties
});
@ -709,12 +709,20 @@
// Player instance
function Plyr(media, config) {
var plyr = this,
timers = {};
timers = {},
api;
// Set media
plyr.media = media;
var original = media.cloneNode(true);
// Trigger events, with plyr instance passed
function _triggerEvent(element, type, bubbles, properties) {
_event(element, type, bubbles, _extend({}, properties, {
plyr: api
}));
}
// Debugging
function _console(type, args) {
if (config.debug && window.console) {
@ -2906,25 +2914,52 @@
first = true,
timer;
// Seek by the number keys
function seekByKey() {
// Get current duration
var duration = plyr.media.duration;
// Bail if we have no duration set
if (!_is.number(duration)) {
return;
}
// Divide the max duration into 10th's and times by the number value
_seek((duration / 10) * (code - 48));
}
function handleKey() {
console.log(code);
switch(code) {
// 0-9
case 48:
case 49:
case 50:
case 51:
case 52:
case 53:
case 54:
case 55:
case 56:
case 57: if (first) { seekByKey() } break;
// Space and K key
case 32:
case 75: if (first) { _togglePlay(); } break;
case 75: if (first) { _togglePlay() } break;
// Arrow up
case 38: _increaseVolume(); break;
// Arrow down
case 40: _decreaseVolume(); break;
// M key
case 77: if (first) { _toggleMute(); } break;
case 77: if (first) { _toggleMute() } break;
// Arrow forward
case 39: _forward(); break;
// Arrow back
case 37: _rewind(); break;
// F key
case 70: if (first) { _toggleFullscreen(); } break;
case 70: if (first) { _toggleFullscreen() } break;
// C key
case 67: if (first) { _toggleCaptions(); } break;
case 67: if (first) { _toggleCaptions() } break;
}
// Escape is handle natively when in full screen
@ -3399,7 +3434,7 @@
_displayDuration();
}
var api = {
api = {
getOriginal: function() { return original; },
getContainer: function() { return plyr.container },
getEmbed: function() { return plyr.embed; },
@ -3636,12 +3671,12 @@
var events = config.events.concat(['setup', 'ready', 'statechange', 'enterfullscreen', 'exitfullscreen', 'captionsenabled', 'captionsdisabled']);
_on(instance.getContainer(), events.join(' '), function() {
console.log([config.logPrefix, 'event:', event.type].join(' '));
console.log([config.logPrefix, 'event:', event.type].join(' '), event.detail.plyr);
});
}
// Callback
_triggerEvent(instance.getContainer(), 'setup', true, {
_event(instance.getContainer(), 'setup', true, {
plyr: instance
});