I have an embedded Vimeo video on the homepage my site, which is set to autoplay when the site loads. I am also using the api and froogaloop.js
for some custom controls.
What I need to do is save the time the video has got to when a visitor navigates to another page, then resume playing from this point if and when they return to the homepage.
I know I can use playProgress
to get the time elapsed, but am not sure how to store this and how to use it when the visitor returns to the homepage.
EDIT
I now have the following code, and am using js-cookie to store the progress cookie. How would I get the value of playProgress
and set it as a cookie using beforeunload
on window
? I am not great at javascript so help would be great!
JAVASCRIPT (also including this library https://github.com/js-cookie/js-cookie)
$(function() {
var iframe = $('#player1')[0];
var player1 = $f(iframe);
var status1 = $('.status1');
// When the player is ready, add listener for playProgress
player1.addEvent('ready', function() {
status1.text('ready');
player1.addEvent('playProgress', onPlayProgress);
});
function onPlayProgress(data, id) {
status1.text(data.seconds + ' seconds played');
};
// SETTING A COOKIE
Cookies.set('timeElapsed','something');
});
HTML
<script src="https://f.vimeocdn.com/js/froogaloop2.min.js"></script>
<div class="videoholder">
<h3>Player 1</h3>
<iframe id="player1" src="https://player.vimeo.com/video/142216434?api=1&player_id=player1" width="500" height="281" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
<div>
<p><span class="status1">…</span></p>
</div>
</div>
you can't reliably detect when someone leaves a page. but for the times when it does actually work, simply use the onunload or whatever mechanism to capture the video's current position, stuff it into a cookie, and put it back into the player if/when the user returns.
We count an impression every time the Vimeo player loads your video, either on vimeo.com or embedded, and we count a view every time someone hits the play button on your video.
Go to Vimeo and find the video you want to embed. Once you are on the page, locate the share button on or below the video. After selecting Share a new window will open. The copied Embed Code can then be pasted in the Embed URL field for the Gallery Video content type.
All the options listed above can be found by navigating to your video's Settings > Interaction tools > After video. Open the End screen drop-down menu to choose your end screen.
You can simply read the value of the cookie saved using
function onPlayProgress(data, id) {
Cookies.set('timeElapsed', data.seconds);
status1.text(Cookies.get('timeElapsed') + ' seconds played');
}
and then set the value of the video when it is being loaded by appending &t=0m0s
at the end of the url.
$('#player1').attr('src','https://player.vimeo.com/video/142216434?api=1&player_id=player1&t='+timeElapsed)
I got this working with the following javascript:
$(function() {
var iframe = $('#player1')[0];
var player1 = $f(iframe);
// When the player is ready, add listener for playProgress
player1.addEvent('ready', function() {
player1.api('seekTo',(Cookies.get('timeElapsed')));
player1.api('pause');
player1.addEvent('playProgress', onPlayProgress);
});
function onPlayProgress(data, id) {
Cookies.set('timeElapsed', data.seconds);
};
});
My only issue now is that when you return to the (paused) video, you need to click twice to get it to resume as the Pause button is visible. Does anyone know why this would be? This is the last piece of my Vimeo puzzle!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With