UNPKG

stream-chat-react-native-core

Version:

The official React Native and Expo components for Stream Chat, a service for building chat applications

183 lines 4.97 kB
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.VideoPlayer = exports.INITIAL_VIDEO_PLAYER_STATE = void 0; var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _streamChat = require("stream-chat"); var _native = require("../native"); var _constants = require("../utils/constants"); var DEFAULT_PLAYBACK_RATES = [1.0, 1.5, 2.0]; var INITIAL_VIDEO_PLAYER_STATE = exports.INITIAL_VIDEO_PLAYER_STATE = { duration: 0, isPlaying: false, position: 0, progress: 0, currentPlaybackRate: 1.0, playbackRates: DEFAULT_PLAYBACK_RATES }; var VideoPlayer = exports.VideoPlayer = function () { function VideoPlayer(options) { (0, _classCallCheck2.default)(this, VideoPlayer); this.playerRef = null; this._pool = null; this.initPlayer = ({ playerRef }) => { this.playerRef = playerRef ?? null; }; this.isExpoCLI = _native.NativeHandlers.SDK === 'stream-chat-expo'; this.options = options; this._id = options.id; var playbackRates = options.playbackRates ?? DEFAULT_PLAYBACK_RATES; this.state = new _streamChat.StateStore({ ...INITIAL_VIDEO_PLAYER_STATE, isPlaying: options.autoPlay ?? false, currentPlaybackRate: playbackRates[0], playbackRates }); } return (0, _createClass2.default)(VideoPlayer, [{ key: "id", get: function () { return this._id; } }, { key: "isPlaying", get: function () { return this.state.getLatestValue().isPlaying; }, set: function (isPlaying) { this.state.partialNext({ isPlaying }); } }, { key: "duration", get: function () { return this.state.getLatestValue().duration; }, set: function (duration) { this.state.partialNext({ duration }); } }, { key: "position", get: function () { return this.state.getLatestValue().position; }, set: function (position) { this.state.partialNext({ position, progress: position / this.duration }); } }, { key: "progress", get: function () { return this.state.getLatestValue().progress; }, set: function (progress) { this.state.partialNext({ position: progress * this.duration, progress }); } }, { key: "playbackRates", get: function () { return this.state.getLatestValue().playbackRates; } }, { key: "currentPlaybackRate", get: function () { return this.state.getLatestValue().currentPlaybackRate; } }, { key: "pool", set: function (pool) { this._pool = pool; } }, { key: "changePlaybackRate", value: function changePlaybackRate() { var currentPlaybackRateIndex = this.playbackRates.indexOf(this.currentPlaybackRate); if (currentPlaybackRateIndex === -1) { currentPlaybackRateIndex = 0; } var nextPlayBackIndex = currentPlaybackRateIndex === this.playbackRates.length - 1 ? 0 : currentPlaybackRateIndex + 1; var nextPlaybackRate = this.playbackRates[nextPlayBackIndex]; this.state.partialNext({ currentPlaybackRate: nextPlaybackRate }); } }, { key: "play", value: function play() { if (this._pool) { this._pool.requestPlay(this.id); } if (this.playerRef?.play) { this.playerRef.play(); } this.state.partialNext({ isPlaying: true }); } }, { key: "pause", value: function pause() { if (this.playerRef?.pause) { this.playerRef.pause(); } this.state.partialNext({ isPlaying: false }); if (this._pool) { this._pool.notifyPaused(); } } }, { key: "toggle", value: function toggle() { this.isPlaying ? this.pause() : this.play(); } }, { key: "seek", value: function seek(position) { this.position = position; var positionInSeconds = position / _constants.ONE_SECOND_IN_MILLISECONDS; if (this.isExpoCLI) { if (this.playerRef) { this.playerRef.currentTime = positionInSeconds; } } else { if (this.playerRef?.seek) { this.playerRef.seek(positionInSeconds); } } } }, { key: "stop", value: function stop() { this.seek(0); this.pause(); } }, { key: "onRemove", value: function onRemove() { if (this.playerRef?.pause) { this.playerRef.pause(); } this.playerRef = null; this.state.partialNext({ ...INITIAL_VIDEO_PLAYER_STATE, currentPlaybackRate: this.playbackRates[0], playbackRates: DEFAULT_PLAYBACK_RATES }); } }]); }(); //# sourceMappingURL=video-player.js.map