UNPKG

@sendbird/uikit-react-native

Version:

Sendbird UIKit for React Native: A feature-rich and customizable chat UI kit with messaging, channel management, and user authentication.

242 lines (241 loc) 8.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _uikitUtils = require("@sendbird/uikit-utils"); var _expoBackwardUtils = _interopRequireDefault(require("../utils/expoBackwardUtils")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } class BaseAudioPlayerAdapter { constructor() { _defineProperty(this, "uri", void 0); _defineProperty(this, "state", 'idle'); _defineProperty(this, "playbackSubscribers", new Set()); _defineProperty(this, "stateSubscribers", new Set()); _defineProperty(this, "setState", state => { this.state = state; this.stateSubscribers.forEach(callback => { callback(state); }); }); _defineProperty(this, "requestPermission", async () => { return true; }); _defineProperty(this, "addPlaybackListener", callback => { this.playbackSubscribers.add(callback); return () => { this.playbackSubscribers.delete(callback); }; }); _defineProperty(this, "addStateListener", callback => { this.stateSubscribers.add(callback); return () => { this.stateSubscribers.delete(callback); }; }); } } class LegacyExpoAVPlayerAdapter extends BaseAudioPlayerAdapter { constructor(avModule) { super(); _defineProperty(this, "sound", void 0); _defineProperty(this, "setListener", () => { this.sound.setProgressUpdateIntervalAsync(100).catch(error => { _uikitUtils.Logger.warn('[PlayerService.Expo] Failed to set progress update interval', error); }); this.sound.setOnPlaybackStatusUpdate(status => { if (status.isLoaded) { if (status.didJustFinish) { this.stop().catch(error => { _uikitUtils.Logger.warn('[PlayerService.Expo] Failed to stop in OnPlaybackStatusUpdate', error); }); } if (status.isPlaying) { this.playbackSubscribers.forEach(callback => { callback({ currentTime: status.positionMillis, duration: status.durationMillis ?? 0, stopped: status.didJustFinish }); }); } } }); }); _defineProperty(this, "removeListener", () => { this.sound.setOnPlaybackStatusUpdate(null); }); _defineProperty(this, "prepare", async uri => { this.setState('preparing'); await this.sound.loadAsync({ uri }, { shouldPlay: false }, true); this.uri = uri; }); _defineProperty(this, "play", async uri => { if ((0, _uikitUtils.matchesOneOf)(this.state, ['idle', 'stopped'])) { try { await this.prepare(uri); this.setListener(); await this.sound.playAsync(); this.setState('playing'); } catch (e) { this.setState('idle'); this.uri = undefined; this.removeListener(); throw e; } } else if ((0, _uikitUtils.matchesOneOf)(this.state, ['paused']) && this.uri === uri) { try { this.setListener(); await this.sound.playAsync(); this.setState('playing'); } catch (e) { this.removeListener(); throw e; } } }); _defineProperty(this, "pause", async () => { if ((0, _uikitUtils.matchesOneOf)(this.state, ['playing'])) { await this.sound.pauseAsync(); this.removeListener(); this.setState('paused'); } }); _defineProperty(this, "stop", async () => { if ((0, _uikitUtils.matchesOneOf)(this.state, ['playing', 'paused'])) { await this.sound.stopAsync(); await this.sound.unloadAsync(); this.removeListener(); this.setState('stopped'); } }); _defineProperty(this, "reset", async () => { await this.stop(); this.setState('idle'); this.uri = undefined; this.playbackSubscribers.clear(); this.stateSubscribers.clear(); }); _defineProperty(this, "seek", async time => { if ((0, _uikitUtils.matchesOneOf)(this.state, ['playing', 'paused'])) { await this.sound.playFromPositionAsync(time); } }); this.sound = new avModule.Audio.Sound(); } } class ExpoAudioPlayerAdapter extends BaseAudioPlayerAdapter { constructor(audioModule) { super(); _defineProperty(this, "audioModule", void 0); _defineProperty(this, "player", null); _defineProperty(this, "setListener", () => { if (!this.player) return; this.player.addListener('playbackStatusUpdate', status => { if (status.isLoaded) { if (status.didJustFinish) { this.stop().catch(error => { _uikitUtils.Logger.warn('[PlayerService.Expo] Failed to stop in playbackStatusUpdate', error); }); } if (status.playing) { this.playbackSubscribers.forEach(callback => { callback({ currentTime: status.currentTime, duration: status.duration ?? 0, stopped: status.didJustFinish }); }); } } }); }); _defineProperty(this, "removeListener", () => { if (this.player) { this.player.remove(); } }); _defineProperty(this, "prepare", async uri => { this.setState('preparing'); this.player = this.audioModule.createAudioPlayer(uri, { updateInterval: 100 }); this.uri = uri; }); _defineProperty(this, "play", async uri => { if ((0, _uikitUtils.matchesOneOf)(this.state, ['idle', 'stopped'])) { try { var _this$player; await this.prepare(uri); this.setListener(); (_this$player = this.player) === null || _this$player === void 0 || _this$player.play(); this.setState('playing'); } catch (e) { this.setState('idle'); this.uri = undefined; this.removeListener(); throw e; } } else if ((0, _uikitUtils.matchesOneOf)(this.state, ['paused']) && this.uri === uri) { try { var _this$player2; this.setListener(); (_this$player2 = this.player) === null || _this$player2 === void 0 || _this$player2.play(); this.setState('playing'); } catch (e) { this.removeListener(); throw e; } } }); _defineProperty(this, "pause", async () => { if ((0, _uikitUtils.matchesOneOf)(this.state, ['playing'])) { var _this$player3; (_this$player3 = this.player) === null || _this$player3 === void 0 || _this$player3.pause(); this.removeListener(); this.setState('paused'); } }); _defineProperty(this, "stop", async () => { if ((0, _uikitUtils.matchesOneOf)(this.state, ['playing', 'paused'])) { var _this$player4; (_this$player4 = this.player) === null || _this$player4 === void 0 || _this$player4.pause(); this.removeListener(); this.setState('stopped'); } }); _defineProperty(this, "reset", async () => { var _this$player5; await this.stop(); (_this$player5 = this.player) === null || _this$player5 === void 0 || _this$player5.remove(); this.player = null; this.setState('idle'); this.uri = undefined; this.playbackSubscribers.clear(); this.stateSubscribers.clear(); }); _defineProperty(this, "seek", async time => { if ((0, _uikitUtils.matchesOneOf)(this.state, ['playing', 'paused']) && this.player) { this.player.currentTime = time; } }); this.audioModule = audioModule; } } const createExpoPlayerService = ({ avModule }) => { if (_expoBackwardUtils.default.expoAV.isLegacyAVModule(avModule)) { _uikitUtils.Logger.warn('[PlayerService.Expo] expo-av is deprecated and will be removed in Expo 54. Please migrate to expo-audio.'); } return _expoBackwardUtils.default.expoAV.isAudioModule(avModule) ? new ExpoAudioPlayerAdapter(avModule) : new LegacyExpoAVPlayerAdapter(avModule); }; var _default = exports.default = createExpoPlayerService; //# sourceMappingURL=createPlayerService.expo.js.map