Vimeo block seeking forward v0

by Jacques-Yves Bleau

HTML

<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>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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...