Skip to content

Instantly share code, notes, and snippets.

@ctlcltd
Last active February 12, 2018 15:20
Show Gist options
  • Save ctlcltd/b81b2138994be36dda02d427ec709c60 to your computer and use it in GitHub Desktop.
Save ctlcltd/b81b2138994be36dda02d427ec709c60 to your computer and use it in GitHub Desktop.
Loop trick Dailymotion
<script type="text/javascript" src="https://api.dmcdn.net/all.js"></script>
<div id="player"></div>
<script type="text/javascript">
var DM_player = DM.player(document.getElementById('player'), {
video: 'x32llg2',
width: '100%',
height: '100%',
params: {
autoplay: true,
endscreenEnable: false,
quality: 1080
}
});
window.ltd = {
videoSeekStart: 0,
videoRefreshEnd: 1000,
videoRefreshRatio: 2
}; // in ms.
ltd.videoEnd = function() {
console.log('ltd.videoEnd');
this.seek(ltd.videoSeekStart);
};
ltd.videoReachEnd = function(elapsed, remaining) {
console.log('ltd.videoReachEnd', elapsed, remaining);
if (ltd.videoLoop) {
return;
}
var self = this;
ltd.videoLoop = true;
window.setTimeout(function() {
self.seek(ltd.videoSeekStart);
this.clearTimeout();
}, (parseInt(remaining) / ltd.videoRefreshRatio));
};
ltd.videoReset = function(event) {
console.log('ltd.videoReset');
ltd.videoStart = null;
ltd.videoLoop = null;
};
ltd.videoPlaying = function(event) {
if (! ltd.videoDuration) {
ltd.videoDuration = parseFloat(event.target.duration) * 1000;
}
if (! ltd.videoStart) {
ltd.videoStart = parseFloat(event.timeStamp);
}
var elapsed = (parseFloat(event.timeStamp) - ltd.videoStart);
var remaining = (ltd.videoDuration - elapsed);
if (remaining < ltd.videoRefreshEnd) {
ltd.videoReachEnd.apply(this, [ elapsed, remaining ]);
}
console.log('ltd.videoPlaying', elapsed, remaining);
};
DM_player.addEventListener('video_start', ltd.videoReset);
DM_player.addEventListener('ad_start', ltd.videoReset);
DM_player.addEventListener('seeked', ltd.videoReset);
DM_player.addEventListener('timeupdate', ltd.videoPlaying);
DM_player.addEventListener('end', ltd.videoEnd);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment