playable
Version:
Video player based on HTML5Video
107 lines • 4.08 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var constants_1 = require("../../../../constants");
var StateEngine = /** @class */ (function () {
function StateEngine(eventEmitter, output, controller) {
this._eventEmitter = eventEmitter;
this._remotePlayerController = controller;
this._output = output;
this._currentState = null;
this._isMetadataLoaded = false;
this._bindCallbacks();
this._bindEvents();
}
StateEngine.prototype._bindCallbacks = function () {
this._processEventFromVideo = this._processEventFromVideo.bind(this);
};
StateEngine.prototype._bindEvents = function () {
var _this = this;
var cast = window.cast;
var castEvents = cast.framework.RemotePlayerEventType;
this._remotePlayerController.addEventListener(castEvents.ANY_CHANGE, function (e) {
_this._processEventFromVideo(e);
});
this._remotePlayerController.addEventListener(castEvents.IS_PAUSED_CHANGED, function (e) {
var isPaused = e.value;
_this.setState(isPaused ? constants_1.EngineState.PAUSED : constants_1.EngineState.PLAYING);
_this._processEventFromVideo(e);
});
};
StateEngine.prototype._processEventFromVideo = function (event) {
switch (event.field) {
case 'playerState': {
if (event.value === 'PLAYING') {
this.setState(constants_1.EngineState.PLAYING);
return;
}
if (event.value === 'PAUSED') {
this.setState(constants_1.EngineState.PAUSED);
return;
}
if (event.value === null) {
this.setState(constants_1.EngineState.ENDED);
return;
}
break;
}
case 'volumeLevel':
case 'isMuted':
this._checkVolumeChanges();
break;
case 'mediaInfo':
if (event.value && event.value.duration) {
this._eventEmitter.emitAsync(constants_1.VideoEvent.DURATION_UPDATED, event.value);
}
break;
default:
break;
}
};
StateEngine.prototype._checkVolumeChanges = function () {
var output = this._output;
var newVolume = Math.floor(output.volume * 100);
if (this._currentVolume !== newVolume) {
this._currentVolume = newVolume;
this._eventEmitter.emitAsync(constants_1.VideoEvent.VOLUME_CHANGED, this._currentVolume);
}
if (this._currentMute !== output.isMuted) {
this._currentMute = output.isMuted;
this._eventEmitter.emitAsync(constants_1.VideoEvent.MUTE_CHANGED, this._currentMute);
}
this._eventEmitter.emitAsync(constants_1.VideoEvent.SOUND_STATE_CHANGED, {
volume: this._currentVolume,
muted: this._currentMute,
});
};
StateEngine.prototype.setState = function (state) {
if (state === this._currentState) {
return;
}
this._eventEmitter.emitAsync(constants_1.VideoEvent.STATE_CHANGED, {
prevState: this._currentState,
nextState: state,
});
this._eventEmitter.emitAsync(state);
this._currentState = state;
};
Object.defineProperty(StateEngine.prototype, "isMetadataLoaded", {
get: function () {
return this._isMetadataLoaded;
},
enumerable: false,
configurable: true
});
Object.defineProperty(StateEngine.prototype, "state", {
get: function () {
return this._currentState;
},
enumerable: false,
configurable: true
});
StateEngine.prototype.destroy = function () {
this._eventEmitter = null;
};
return StateEngine;
}());
exports.default = StateEngine;
//# sourceMappingURL=state-engine.js.map