playable
Version:
Video player based on HTML5Video
110 lines • 4.25 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NATIVE_VIDEO_TO_BROADCAST = void 0;
var constants_1 = require("../../../../constants");
exports.NATIVE_VIDEO_TO_BROADCAST = [
'loadstart',
'progress',
'error',
'stalled',
'suspend',
'durationchange',
'timeupdate',
'volumechange',
'seeking',
];
var NativeEventsBroadcaster = /** @class */ (function () {
function NativeEventsBroadcaster(eventEmitter, output) {
this._eventEmitter = eventEmitter;
this._video = output;
this._currentMute = this._video.muted;
this._currentVolume = this._video.volume;
this._bindCallbacks();
this._bindEvents();
}
NativeEventsBroadcaster.prototype._bindCallbacks = function () {
this._processEventFromVideo = this._processEventFromVideo.bind(this);
};
NativeEventsBroadcaster.prototype._bindEvents = function () {
var _this = this;
exports.NATIVE_VIDEO_TO_BROADCAST.forEach(function (event) {
return _this._video.addEventListener(event, _this._processEventFromVideo);
});
};
NativeEventsBroadcaster.prototype._unbindEvents = function () {
var _this = this;
exports.NATIVE_VIDEO_TO_BROADCAST.forEach(function (event) {
return _this._video.removeEventListener(event, _this._processEventFromVideo);
});
};
NativeEventsBroadcaster.prototype._processEventFromVideo = function (event) {
if (event === void 0) { event = {}; }
var video = this._video;
switch (event.type) {
case 'loadstart': {
if (this._shouldCheckVolume) {
this._checkVolumeChanges();
}
break;
}
case 'progress': {
this._eventEmitter.emitAsync(constants_1.VideoEvent.CHUNK_LOADED);
break;
}
case 'stalled': {
this._eventEmitter.emitAsync(constants_1.VideoEvent.UPLOAD_STALLED);
break;
}
case 'suspend': {
this._eventEmitter.emitAsync(constants_1.VideoEvent.UPLOAD_SUSPEND);
break;
}
case 'seeking': {
this._eventEmitter.emitAsync(constants_1.VideoEvent.SEEK_IN_PROGRESS, video.currentTime);
break;
}
case 'durationchange': {
this._eventEmitter.emitAsync(constants_1.VideoEvent.DURATION_UPDATED, video.duration);
break;
}
case 'timeupdate': {
this._eventEmitter.emitAsync(constants_1.VideoEvent.CURRENT_TIME_UPDATED, video.currentTime);
break;
}
case 'volumechange': {
if (this._shouldCheckVolume) {
this._shouldCheckVolume = false;
}
this._checkVolumeChanges();
break;
}
default:
break;
}
};
NativeEventsBroadcaster.prototype._checkVolumeChanges = function () {
var video = this._video;
if (this._currentVolume !== video.volume) {
this._currentVolume = video.volume * 100;
this._eventEmitter.emitAsync(constants_1.VideoEvent.VOLUME_CHANGED, this._currentVolume);
}
if (this._currentMute !== video.muted) {
this._currentMute = video.muted;
this._eventEmitter.emitAsync(constants_1.VideoEvent.MUTE_CHANGED, this._currentMute);
}
this._eventEmitter.emitAsync(constants_1.VideoEvent.SOUND_STATE_CHANGED, {
volume: video.volume,
muted: video.muted,
});
};
//Workaround for problem with HTML5Video not firing volumechange if source changed right after volume/muted changed
NativeEventsBroadcaster.prototype.checkVolumeChangeAfterLoadStart = function () {
this._shouldCheckVolume = true;
};
NativeEventsBroadcaster.prototype.destroy = function () {
this._unbindEvents();
};
return NativeEventsBroadcaster;
}());
exports.default = NativeEventsBroadcaster;
//# sourceMappingURL=native-events-broadcaster.js.map