<h4>Seek if you can (Vimeo Player)</h4>
<p>You won't be able to seek outside what was already played, even on mobile fullscreen mode</p>
<div id="player"></div>
<br>
<i>Video by <a target="_blank" href="https://vimeo.com/stefanforster">Stefan Forster</a></i>
<div style="font-family: courrier, monospace">
<p>currenttime: <span id="player-currenttime"></span><br>
played: <span id="player-played"></span></p>
<h5>Tested on</h5>
<ul>
<li>IE11</li>
<li>Edge 16</li>
<li>Chrome 66+</li>
<li>Safari iOS 11.3.1</li>
</ul>
<h5>Dev Notes:</h5>
<ol>
<li>Code not optimized</li>
<li>Works most of the time but on rare occasions it let the user seeks under somes browsers (when you repeatedly try to seek)</li>
<li>There is a little bug when you seek to the end of the seek bar, it does not get back where it should unless you click elsewhere</li>
<li>it's not good UX to show a clickable seek bar when you cannot seek. The controls player design would have to be adapted. Unfortunately, on Mobile IOS (AFAIK), the video player UI cannot be overriden when in fullscreen.</li>
</ol>
</div>
<script src="https://player.vimeo.com/api/player.js"></script>
JavaScript
var supposedCurrentTime = 0;
var canSeek = false;
var div = document.querySelector('#player');
var player = new Vimeo.Player(div, {
url: 'https://vimeo.com/246856333',
autoplay: true,
muted: true
});
player.on('play', function() {
console.log('play');
});
player.on('pause', function() {
console.log('pause');
});
player.on('ended', function(data) {
console.log("end");
getPlayed().then(function(totalPlayed) {
var playedWithOffset = totalPlayed + 10;
if (playedWithOffset < data.duration) {
return // end event occured from seeking to it.
// Don't consider the video as "completed" and let it auto seek back
}
console.log('can seek now');
canSeek = true;
supposedCurrentTime = 0; // reset state in order to allow for rewind
})
});
function log(value) {
document.getElementById('console').innerHTML += value + '<br>'
console.log(value);
}
var video = document.getElementById('video');
player.on('timeupdate', function(data) {
document.getElementById('player-currenttime').innerText = data.seconds;
getPlayed().then(function(data) {
document.getElementById('player-played').innerText = data;
})
if (canSeek) {
return
}
player.getSeeking().then(function(seeking) {
// seeking = whether the player is seeking or not
if (!seeking) {
supposedCurrentTime = data.seconds;
}
})
});
// prevent user from seeking
player.on('seeking', function(data) {
// NB: Uncomment to see the played ranges
// logPlayedRange();
if (canSeek) {
return
}
// ?? NB: pause first if the position is near the end,
// Because in fullscreen it can still continue to the end sometimes
if (data.duration - data.seconds < 10) {
player.pause()
}
// accept rewind
if (data.seconds < supposedCurrentTime) {
return
}
// accept seek to already played time
isPlayed(data.seconds).then(function(played) {
if (played) {
return
}
// guard agains infinite recursion:
// user...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.