Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix set current time to keep the same behavior for all players #5416

Merged
merged 4 commits into from
Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 22 additions & 9 deletions assets/shared/helpers/player/videopress-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,14 @@ export const initializePlayer = ( element, w = window ) =>

resolve( element );
} else if (
data.event === `videopress_timeupdate` &&
data.event === 'videopress_timeupdate' &&
data.currentTimeMs
) {
// Set the current time to a dataset in order to be available later.
element.dataset.currentTime = data.currentTimeMs / 1000;
} else if ( data.event === 'videopress_play' ) {
// Identify that video was already played.
element.dataset.hasPlayed = 'has-played';
}
};

Expand Down Expand Up @@ -106,14 +109,24 @@ export const getCurrentTime = ( player ) =>
*/
export const setCurrentTime = ( player, seconds ) =>
new Promise( ( resolve ) => {
player.contentWindow.postMessage(
{
event: 'videopress_action_set_currenttime',
currentTime: seconds,
},
'*'
);
resolve();
const run = () => {
player.contentWindow.postMessage(
{
event: 'videopress_action_set_currenttime',
currentTime: seconds,
},
'*'
);
resolve();
};

if ( player.dataset.hasPlayed ) {
run();
} else {
play( player )
.then( () => pause( player ) )
.then( run );
}
} );

/**
Expand Down
28 changes: 24 additions & 4 deletions assets/shared/helpers/player/vimeo-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,20 @@ export const EMBED_PATTERN = /vimeo\.com\/.+/i;
*
* @return {Object} The Vimeo player instance through a promise.
*/
export const initializePlayer = ( element, w = window ) =>
Promise.resolve( new w.Vimeo.Player( element ) );
export const initializePlayer = ( element, w = window ) => {
const player = new w.Vimeo.Player( element );

// Add a dataset to identify if video has played already.
const onPlay = () => {
element.dataset.hasPlayed = 'has-played';
player.off( 'play', onPlay );
};
if ( 'has-played' !== element.dataset.hasPlayed ) {
player.on( 'play', onPlay );
}

return player.ready().then( () => player );
};

/**
* Get the video duration.
Expand Down Expand Up @@ -47,8 +59,16 @@ export const getCurrentTime = ( player ) => player.getCurrentTime();
* @return {Promise} A promise that resolves if the video was set to a current time successfully.
* (original return from Vimeo API).
*/
export const setCurrentTime = ( player, seconds ) =>
player.setCurrentTime( seconds );
export const setCurrentTime = ( player, seconds ) => {
if ( player.element.dataset.hasPlayed ) {
return player.setCurrentTime( seconds );
}

// Play the video a first time if it wasn't already played yet.
return play( player )
.then( () => pause( player ) )
.then( () => player.setCurrentTime( seconds ) );
};

/**
* Play the video.
Expand Down
38 changes: 32 additions & 6 deletions assets/shared/helpers/player/youtube-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ export const ADAPTER_NAME = 'youtube';
*/
export const EMBED_PATTERN = /(youtu\.be|youtube\.com)\/.+/i;

let lastTime;

/**
* Initialize the player.
*
Expand All @@ -33,6 +31,20 @@ export const initializePlayer = ( element, w = window ) =>
} else {
player.addEventListener( 'onReady', onReady );
}

// Add a dataset to identify if video has played already.
const onStateChange = ( e ) => {
if ( e.data === w.YT.PlayerState.PLAYING ) {
element.dataset.hasPlayed = 'has-played';
player.removeEventListener(
'onStateChange',
onStateChange
);
}
};
if ( 'has-played' !== element.dataset.hasPlayed ) {
player.addEventListener( 'onStateChange', onStateChange );
}
} );
} );

Expand Down Expand Up @@ -70,8 +82,17 @@ export const getCurrentTime = ( player ) =>
*/
export const setCurrentTime = ( player, seconds ) =>
new Promise( ( resolve ) => {
player.seekTo( seconds );
resolve();
if ( player.i.dataset.hasPlayed ) {
player.seekTo( seconds );
resolve();
} else {
play( player )
.then( () => pause( player ) )
.then( () => {
player.seekTo( seconds );
resolve();
} );
}
} );

/**
Expand Down Expand Up @@ -100,6 +121,11 @@ export const pause = ( player ) =>
resolve();
} );

/**
* Variable that saves the last current time.
*/
let lastCurrentTime;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was just moved, renamed, and documented.


/**
* Add an timeupdate event listener to the player.
*
Expand All @@ -113,9 +139,9 @@ export const onTimeupdate = ( player, callback, w = window ) => {
const timer = 250;

const updateCurrentTime = ( currentTime ) => {
if ( lastTime !== currentTime ) {
if ( lastCurrentTime !== currentTime ) {
callback( currentTime );
lastTime = currentTime;
lastCurrentTime = currentTime;
}
};

Expand Down