Skip to content

Commit

Permalink
feat(player): add seekableRanges helper function
Browse files Browse the repository at this point in the history
Adds a `seekableRanges` function which is particularly useful to get an array of
ranges representing the portions of media that the user agent is able to seek
to. `seekableRanges` uses the `seekable` function native to `HTMLMediaElement`.

- add seekableRanges function to the player
- add test coverage
  • Loading branch information
amtins committed Mar 28, 2024
1 parent 63cf4ee commit 5030afe
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/components/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,26 @@ class Player extends vjsPlayer {
return ranges;
}

/**
* Calculates an array of ranges based on the `seekable()` data.
*
* @see https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/seekable
*
* @returns {Array<{start: number, end: number}>} An array of objects representing start and end points of seekable ranges.
*/
seekableRanges() {
const ranges = [];

for (let i = 0; i < this.seekable().length; i++) {
const start = this.seekable().start(i);
const end = this.seekable().end(i);

ranges.push({ start, end });
}

return ranges;
}

/**
* A getter/setter for the media's text track.
* Activates the text track according to the language and kind properties.
Expand Down
31 changes: 31 additions & 0 deletions test/components/player.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,37 @@ describe('Player', () => {
});
});

describe('seekableRanges', () => {
it('should return an empty array if there are no seekable ranges', () => {
const player = new Player(videoEl);

player.seekable = jest.fn().mockImplementation(() => {
return pillarbox.time.createTimeRanges();
});

expect(player.seekableRanges()).toHaveLength(0);
});

it('should return an array containing two entries', () => {
const player = new Player(videoEl);

player.seekable = jest.fn().mockImplementation(() => {
return pillarbox.time.createTimeRanges([
[0, 10],
[11, 69]
]);
});

expect(player.seekableRanges()).toHaveLength(2);
expect(player.seekableRanges()).toEqual(
expect.arrayContaining([
{ 'end': 10, 'start': 0 },
{ 'end': 69, 'start': 11 }
])
);
});
});

describe('textTrack', () => {
const player = new Player(videoEl);

Expand Down

0 comments on commit 5030afe

Please sign in to comment.