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.

136 lines (135 loc) 5.22 kB
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); } import { Platform } from 'react-native'; import { Logger, matchesOneOf, sleep } from '@sendbird/uikit-utils'; const createNativePlayerService = ({ audioRecorderModule, permissionModule }) => { const module = new audioRecorderModule.default(); class VoicePlayer { 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, "setListener", () => { module.addPlayBackListener(async data => { const stopped = data.currentPosition >= data.duration; if (stopped) { this.stop().catch(error => { Logger.warn('[PlayerService.Native] Failed to stop in PlayBackListener', error); }); } if (this.state === 'playing') { this.playbackSubscribers.forEach(callback => { callback({ currentTime: data.currentPosition, duration: data.duration, stopped }); }); } }); }); _defineProperty(this, "removeListener", () => { module.removePlayBackListener(); }); _defineProperty(this, "requestPermission", async () => { if (Platform.OS === 'android') { if (Platform.Version > 32) return true; const { READ_EXTERNAL_STORAGE } = permissionModule.PERMISSIONS.ANDROID; const status = await permissionModule.check(READ_EXTERNAL_STORAGE); if (status === 'granted') { return true; } else { const status = await permissionModule.request(READ_EXTERNAL_STORAGE); return status === 'granted'; } } else { 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); }; }); _defineProperty(this, "play", async uri => { if (matchesOneOf(this.state, ['idle', 'stopped'])) { try { this.setState('preparing'); this.uri = uri; this.setListener(); // FIXME: Workaround, `module.startPlayer()` caused a significant frame-drop and prevented the 'preparing' UI transition. await sleep(0); await module.startPlayer(uri); this.setState('playing'); } catch (e) { this.setState('idle'); this.uri = undefined; this.removeListener(); throw e; } } else if (matchesOneOf(this.state, ['paused']) && this.uri === uri) { try { this.setListener(); await module.resumePlayer(); this.setState('playing'); } catch (e) { this.removeListener(); throw e; } } }); _defineProperty(this, "pause", async () => { if (matchesOneOf(this.state, ['playing'])) { await module.pausePlayer(); this.removeListener(); this.setState('paused'); } }); _defineProperty(this, "stop", async () => { if (matchesOneOf(this.state, ['preparing', 'playing', 'paused'])) { await module.stopPlayer(); 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 (matchesOneOf(this.state, ['playing', 'paused'])) { await module.seekToPlayer(time); } }); module.setSubscriptionDuration(0.1).catch(error => { Logger.warn('[PlayerService.Native] Failed to set subscription duration', error); }); } } return new VoicePlayer(); }; export default createNativePlayerService; //# sourceMappingURL=createPlayerService.native.js.map