Skip to content

Commit

Permalink
Added seek event fixes (fixes #409), Added support for URLs (fixes #345)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Potts committed Nov 2, 2016
1 parent 10561d6 commit efe54fb
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 37 deletions.
2 changes: 1 addition & 1 deletion demo/dist/demo.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion demo/src/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
// Set a new source
function newSource(type, init) {
// Bail if new type isn't known, it's the current type, or current type is empty (video is default) and new type is video
if(!(type in types) || (!init && type == currentType) || (!currentType.length && type == types.video)) {
if(!(type in types) || (!init && type === currentType) || (!currentType.length && type === types.video)) {
return;
}

Expand Down
4 changes: 2 additions & 2 deletions dist/plyr.js

Large diffs are not rendered by default.

36 changes: 26 additions & 10 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,22 @@ Oh and yes, it works with Bootstrap.
## Changelog
Check out the [changelog](changelog.md) to see what's new with Plyr.

## Planned Development
- Playback speed
- Quality selection
## Features currently being developed
- Playback speed (#53)
- Quality selection (#218)
- Caption language selection (#385)
- AirPlay
- Picture in Picture (MacOS Sierra + Safari) (#366)
[more info](https://github.com/Selz/plyr/issues?q=is%3Aissue+is%3Aopen+label%3A%22In+Development%22)

## Planned features
- Playlists
- Multiple language captions (with selection)
- Google cast
- Facebook video support
- Wistia video support
- YouTube and Vimeo audio support
- Audio captions
... and whatever else has been raised in [issues](https://github.com/Selz/plyr/issues)
...and whatever else has been raised in [issues](https://github.com/Selz/plyr/issues)

If you have any cool ideas or features, please let me know by [creating an issue](https://github.com/Selz/plyr/issues/new) or, of course, forking and sending a pull request.

Expand Down Expand Up @@ -106,10 +115,13 @@ For YouTube and Vimeo, Plyr uses the standard YouTube API markup (an empty `<div
<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
```html
<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](#initialising).
Expand Down Expand Up @@ -676,6 +688,8 @@ player.source({
});
```

Note: `src` can be the video ID or URL

Vimeo example

```javascript
Expand All @@ -689,7 +703,9 @@ player.source({
});
```

Some more details on the object parameters
Note: `src` can be the video ID or URL

More details on the object parameters

<table class="table" width="100%">
<thead>
Expand All @@ -713,7 +729,7 @@ Some more details on the object parameters
<tr>
<td><code>sources</code></td>
<td>Array</td>
<td>This is an array of sources. <code>type</code> is optional for YouTube and Vimeo when specifying an array. For YouTube and Vimeo media, only the video ID must be passed as the source as shown above. The keys of this object are mapped directly to HTML attributes so more can be added to the object if required.</td>
<td>This is an array of sources. <code>type</code> is optional for YouTube and Vimeo when specifying an array. For YouTube and Vimeo media, the video ID or URL must be passed as the source as shown above. The keys of this object are mapped directly to HTML attributes so more can be added to the object if required.</td>
</tr>
<tr>
<td><code>poster</code></td>
Expand Down Expand Up @@ -776,7 +792,7 @@ These events also bubble up the DOM. The event target will be the container elem
<tr>
<td><code>ended</code></td>
<td></td>
<td>Sent when playback completes.</td>
<td>Sent when playback completes. Note: with Vimeo this does not occur if `loop` is enabled.</td>
</tr>
<tr>
<td><code>error</code></td>
Expand Down Expand Up @@ -820,12 +836,12 @@ These events also bubble up the DOM. The event target will be the container elem
</tr>
<tr>
<td><code>seeked</code></td>
<td></td>
<td></td>
<td>Sent when a seek operation completes.</td>
</tr>
<tr>
<td><code>seeking</code></td>
<td></td>
<td></td>
<td>Sent when a seek operation begins.</td>
</tr>
<tr>
Expand Down
67 changes: 44 additions & 23 deletions src/js/plyr.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@
fullscreen: null
},
// Events to watch on HTML5 media elements
events: ['ready', 'ended', 'progress', 'stalled', 'playing', 'waiting', 'canplay', 'canplaythrough', 'loadstart', 'loadeddata', 'loadedmetadata', 'timeupdate', 'volumechange', 'play', 'pause', 'error', 'seeking', 'emptied'],
events: ['ready', 'ended', 'progress', 'stalled', 'playing', 'waiting', 'canplay', 'canplaythrough', 'loadstart', 'loadeddata', 'loadedmetadata', 'timeupdate', 'volumechange', 'play', 'pause', 'error', 'seeking', 'seeked', 'emptied'],
// Logging
logPrefix: '[Plyr]'
};
Expand Down Expand Up @@ -596,6 +596,18 @@
}
};

// Parse YouTube ID from url
function _parseYouTubeId(url) {
var regex = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/;
return (url.match(regex)) ? RegExp.$2 : url;
}

// Parse Vimeo ID from url
function _parseVimeoId(url) {
var regex = /^.*(vimeo.com\/|video\/)(\d+).*/;
return (url.match(regex)) ? RegExp.$2 : url;
}

// Fullscreen API
function _fullscreen() {
var fullscreen = {
Expand Down Expand Up @@ -1505,9 +1517,23 @@
// Setup YouTube/Vimeo
function _setupEmbed() {
var container = document.createElement('div'),
mediaId = plyr.embedId,
mediaId,
id = plyr.type + '-' + Math.floor(Math.random() * (10000));

// Parse IDs from URLs if supplied
switch (plyr.type) {
case 'youtube':
mediaId = _parseYouTubeId(plyr.embedId);
break;

case 'vimeo':
mediaId = _parseVimeoId(plyr.embedId);
break;

default:
mediaId = plyr.embedId;
}

// Remove old containers
var containers = _getElements('[id^="' + plyr.type + '-"]');
for (var i = containers.length - 1; i >= 0; i--) {
Expand Down Expand Up @@ -1724,6 +1750,12 @@

case 1:
plyr.media.paused = false;

// If we were seeking, fire seeked event
if (plyr.media.seeking) {
_triggerEvent(plyr.media, 'seeked');
}

plyr.media.seeking = false;
_triggerEvent(plyr.media, 'play');
_triggerEvent(plyr.media, 'playing');
Expand Down Expand Up @@ -1848,6 +1880,12 @@
}
});

plyr.embed.on('seeked', function() {
plyr.media.seeking = false;
_triggerEvent(plyr.media, 'seeked');
_triggerEvent(plyr.media, 'play');
});

plyr.embed.on('ended', function() {
plyr.media.paused = true;
_triggerEvent(plyr.media, 'ended');
Expand Down Expand Up @@ -2009,7 +2047,6 @@

// Embeds
if (_inArray(config.types.embed, plyr.type)) {
// YouTube
switch(plyr.type) {
case 'youtube':
plyr.embed.seekTo(targetTime);
Expand All @@ -2029,11 +2066,14 @@
_pause();
}

// Trigger timeupdate for embeds
// Trigger timeupdate
_triggerEvent(plyr.media, 'timeupdate');

// Set seeking flag
plyr.media.seeking = true;

// Trigger seeking
_triggerEvent(plyr.media, 'seeking');
}

// Logging
Expand Down Expand Up @@ -3259,19 +3299,6 @@
}
}

// Taken from https://gist.github.com/takien/4077195
function parseYoutubeVideoId(url) {
var videoId;
url = url.replace(/(>|<)/gi,'').split(/(vi\/|v=|\/v\/|youtu\.be\/|\/embed\/)/);
if(url[2] !== undefined) {
videoId = url[2].split(/[^0-9a-z_\-]/i);
videoId = videoId[0];
} else {
videoId = url;
}
return videoId;
}

// Setup a player
function _init() {
// Bail if the element is initialized
Expand Down Expand Up @@ -3300,12 +3327,6 @@
plyr.type = media.getAttribute('data-type');
plyr.embedId = media.getAttribute('data-video-id');

switch(plyr.type) {
case 'youtube':
plyr.embedId = parseYoutubeVideoId(plyr.embedId);
break;
}

// Clean up
media.removeAttribute('data-type');
media.removeAttribute('data-video-id');
Expand Down

0 comments on commit efe54fb

Please sign in to comment.