UNPKG

stream-chat-react-native-core

Version:

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

154 lines 4.25 kB
Object.defineProperty(exports, "__esModule", { value: true }); exports.VideoPlayer = exports.INITIAL_VIDEO_PLAYER_STATE = void 0; 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 }; class VideoPlayer { playerRef = null; _pool = null; constructor(options) { var _options$playbackRate, _options$autoPlay; this.isExpoCLI = _native.NativeHandlers.SDK === 'stream-chat-expo'; this.options = options; this._id = options.id; var playbackRates = (_options$playbackRate = options.playbackRates) != null ? _options$playbackRate : DEFAULT_PLAYBACK_RATES; this.state = new _streamChat.StateStore({ ...INITIAL_VIDEO_PLAYER_STATE, isPlaying: (_options$autoPlay = options.autoPlay) != null ? _options$autoPlay : false, currentPlaybackRate: playbackRates[0], playbackRates }); } initPlayer = ({ playerRef }) => { this.playerRef = playerRef != null ? playerRef : null; }; get id() { return this._id; } get isPlaying() { return this.state.getLatestValue().isPlaying; } get duration() { return this.state.getLatestValue().duration; } get position() { return this.state.getLatestValue().position; } get progress() { return this.state.getLatestValue().progress; } get playbackRates() { return this.state.getLatestValue().playbackRates; } get currentPlaybackRate() { return this.state.getLatestValue().currentPlaybackRate; } set pool(pool) { this._pool = pool; } set duration(duration) { this.state.partialNext({ duration }); } set position(position) { this.state.partialNext({ position, progress: position / this.duration }); } set progress(progress) { this.state.partialNext({ position: progress * this.duration, progress }); } set isPlaying(isPlaying) { this.state.partialNext({ isPlaying }); } 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 }); } play() { var _this$playerRef; if (this._pool) { this._pool.requestPlay(this.id); } if ((_this$playerRef = this.playerRef) != null && _this$playerRef.play) { this.playerRef.play(); } this.state.partialNext({ isPlaying: true }); } pause() { var _this$playerRef2; if ((_this$playerRef2 = this.playerRef) != null && _this$playerRef2.pause) { this.playerRef.pause(); } this.state.partialNext({ isPlaying: false }); if (this._pool) { this._pool.notifyPaused(); } } toggle() { this.isPlaying ? this.pause() : this.play(); } seek(position) { this.position = position; var positionInSeconds = position / _constants.ONE_SECOND_IN_MILLISECONDS; if (this.isExpoCLI) { if (this.playerRef) { this.playerRef.currentTime = positionInSeconds; } } else { var _this$playerRef3; if ((_this$playerRef3 = this.playerRef) != null && _this$playerRef3.seek) { this.playerRef.seek(positionInSeconds); } } } stop() { this.seek(0); this.pause(); } onRemove() { var _this$playerRef4; if ((_this$playerRef4 = this.playerRef) != null && _this$playerRef4.pause) { this.playerRef.pause(); } this.playerRef = null; this.state.partialNext({ ...INITIAL_VIDEO_PLAYER_STATE, currentPlaybackRate: this.playbackRates[0], playbackRates: DEFAULT_PLAYBACK_RATES }); } } exports.VideoPlayer = VideoPlayer; //# sourceMappingURL=video-player.js.map