Skip to content

Commit

Permalink
Merge pull request videojs#71 from brightcove/repeat
Browse files Browse the repository at this point in the history
Add repeat functionality to plugin
  • Loading branch information
ldayananda authored Apr 3, 2017
2 parents 7422d53 + cda704b commit 45109f9
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
21 changes: 21 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- [`player.playlist.next() -> Object`](#playerplaylistnext---object)
- [`player.playlist.previous() -> Object`](#playerplaylistprevious---object)
- [`player.playlist.autoadvance([Number delay]) -> undefined`](#playerplaylistautoadvancenumber-delay---undefined)
- [`player.playlist.repeat([Boolean val]) -> Boolean`](#playerplaylistrepeatboolean-val---boolean)
- [Events](#events)
- [`playlistchange`](#playlistchange)
- [`beforeplaylistitem`](#beforeplaylistitem)
Expand Down Expand Up @@ -261,6 +262,26 @@ player.playlist.autoadvance(5);
player.playlist.autoadvance();
```

#### `player.playlist.repeat([Boolean val]) -> Boolean`

Enable or disable repeat by passing true or false as the argument.

When repeat is enabled, the "next" video after the final video in the playlist is the first video in the playlist. This affects the behavior of calling `next()`, of autoplay, and so on.

This method returns the current value. Call with no argument to use as a getter.

Examples:

```
player.repeat(true);
```
```
player.repeat(false);
```
```
var repeat = player.repeat();
```

## Events

### `playlistchange`
Expand Down
40 changes: 36 additions & 4 deletions src/playlist-maker.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import videojs from 'video.js';
import window from 'global/window';
import playItem from './play-item';
import * as autoadvance from './auto-advance';
Expand Down Expand Up @@ -146,6 +147,7 @@ const factory = (player, initialList, initialIndex = 0) => {
currentIndex_: -1,
player_: player,
autoadvance_: {},
repeat_: false,

/**
* Get or set the current item in the playlist.
Expand Down Expand Up @@ -248,11 +250,23 @@ const factory = (player, initialList, initialIndex = 0) => {
*/
next() {

// Make sure we don't go past the end of the playlist.
let index = Math.min(playlist.currentIndex_ + 1, list.length - 1);
let nextIndex;

if (index !== playlist.currentIndex_) {
return list[playlist.currentItem(index)];
// Repeat
if (playlist.repeat_) {
nextIndex = playlist.currentIndex_ + 1;
if (nextIndex > list.length - 1) {
nextIndex = 0;
}

// Don't go past the end of the playlist.
} else {
nextIndex = Math.min(playlist.currentIndex_ + 1, list.length - 1);
}

// Make the change
if (nextIndex !== playlist.currentIndex_) {
return list[playlist.currentItem(nextIndex)];
}
},

Expand Down Expand Up @@ -281,7 +295,25 @@ const factory = (player, initialList, initialIndex = 0) => {
autoadvance(delay) {
playlist.autoadvance_.delay = delay;
autoadvance.setup(playlist.player_, delay);
},

/**
* Sets `repeat` option, which makes the "next" video of the last video in the
* playlist be the first video in the playlist.
*
* @param {Boolean} val
*/
repeat(val) {
if (val !== undefined) {
if (typeof val !== 'boolean') {
videojs.log.error('Invalid value for repeat', val);
} else {
playlist.repeat_ = val;
}
}
return playlist.repeat_;
}

});

playlist.currentItem(initialIndex);
Expand Down

0 comments on commit 45109f9

Please sign in to comment.