Skip to content

Commit

Permalink
feat(cxl-ui): [cxl-jw-player] fix saving position when using a playlist
Browse files Browse the repository at this point in the history
  • Loading branch information
anoblet committed Mar 8, 2023
1 parent 3f024f4 commit 59fa864
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ export function BaseMixin(BaseClass) {
* Each mixin has the ability to hook onto this method.
*/
// eslint-disable-next-line class-methods-use-this, no-unused-vars, no-empty-function
async _onReadyListener() {}
// eslint-disable-next-line class-methods-use-this, no-unused-vars, no-empty-function
async _onTimeListener(event) {}
Expand Down Expand Up @@ -193,7 +196,11 @@ export function BaseMixin(BaseClass) {
});
await new Promise((resolve) => {
this._jwPlayer.on('ready', resolve);
this._jwPlayer.on('ready', async () => {
await this._onReadyListener();
resolve();
});
});
this._jwPlayerContainer = this._jwPlayer.getContainer();
Expand Down
60 changes: 33 additions & 27 deletions packages/cxl-ui/src/components/cxl-jw-player/mixins/StateMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,26 @@ export function StateMixin(BaseClass) {

_userId;

async _index() {
if (this.playlistId) {
const index =
localStorage.getItem(`cxl-jw-player-${this.playlistId}-index`) ||
this._jwPlayer.getPlaylistIndex();

this._jwPlayer.playlistItem(index);

this._jwPlayer.on('playlistItem', async ({ index }) => {
localStorage.setItem(`cxl-jw-player-${this.playlistId}-index`, index);
});
}
}

async _onReadyListener() {
await this._index();
this._position();
this._playbackRate();
}

async _setup() {
await super._setup();

Expand All @@ -14,9 +34,6 @@ export function StateMixin(BaseClass) {
if (typeof window.cxl_pum_vars !== 'undefined') {
this._nonce = window.cxl_pum_vars.nonce;
}

this._playbackRate();
this._position();
}

_playbackRate() {
Expand All @@ -32,39 +49,28 @@ export function StateMixin(BaseClass) {
}

_position() {
const mediaId = this.mediaId || this._jwPlayer.getPlaylistItem().mediaId;

const position = localStorage.getItem(`cxl-jw-player-${mediaId}-position`);
if (this.mediaId) {
this._setPosition();
}

if (position) {
if (this.mediaId) {
this._setPosition(position);
} else {
this._jwPlayer.on('playlistItem', ({ index }) => {
localStorage.setItem(`cxl-jw-player-${this.playlistId}-index`, index);

// Wait for the player to load the new playlist item
setTimeout(() => {
this._setPosition(position);
}, 1000);
});

const index =
localStorage.getItem(`cxl-jw-player-${this.playlistId}-index`) ||
this._jwPlayer.getPlaylistIndex();

this._jwPlayer.playlistItem(index);
}
if (this.playlistId) {
this._jwPlayer.on('playlistItem', async ({ index }) => {
await jwplayer().getPlaylistItemPromise(index);
this._setPosition();
});
}

this._jwPlayer.on('seek time', ({ position }) => {
const mediaId = this.mediaId || this._jwPlayer.getPlaylistItem().mediaid;
localStorage.setItem(`cxl-jw-player-${mediaId}-position`, position);
});
}

_setPosition(position) {
_setPosition() {
const mediaId = this.mediaId || this._jwPlayer.getPlaylistItem().mediaid;
const position = localStorage.getItem(`cxl-jw-player-${mediaId}-position`);

this._jwPlayer.seek(Number(position));
this._jwPlayer.pause();
}
}

Expand Down

0 comments on commit 59fa864

Please sign in to comment.