@alecefe/platzimediaplayer
Version:
An implementation of a MediaPlayer through the Professional Javascript Course
37 lines (36 loc) • 1.24 kB
JavaScript
"use strict";
exports.__esModule = true;
var AutoPause = /** @class */ (function () {
function AutoPause() {
this.threshold = 0.25;
this.handlerIntersection = this.handlerIntersection.bind(this);
this.handlerVisibilityChange = this.handlerVisibilityChange.bind(this);
}
AutoPause.prototype.run = function (player) {
this.player = player;
var observer = new IntersectionObserver(this.handlerIntersection, { threshold: this.threshold });
observer.observe(this.player.media);
document.addEventListener("visibilitychange", this.handlerVisibilityChange);
};
AutoPause.prototype.handlerVisibilityChange = function () {
var isVisible = document.visibilityState === 'visible';
if (isVisible) {
this.player.play();
}
else {
this.player.pause();
}
};
AutoPause.prototype.handlerIntersection = function (entries) {
var entry = entries[0];
var isVisible = entry.intersectionRatio >= this.threshold;
if (isVisible) {
this.player.play();
}
else {
this.player.pause();
}
};
return AutoPause;
}());
exports["default"] = AutoPause;