@exode-team/react-recorder 1.1.1 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -144,6 +144,7 @@ const Messages = () => {
144
144
  | `src` | `string` | ✅ | — | URL аудиофайла. |
145
145
  | `chainId` | `number` | — | — | ID для последовательного воспроизведения. При окончании автоматически запускает следующий. |
146
146
  | `duration` | `number` | — | — | Длительность в секундах (отображается до загрузки аудио). |
147
+ | `autoPreload` | `boolean` | — | `true` | Автозагрузка аудио при попадании в viewport (IntersectionObserver). |
147
148
  | `waveformData` | `number[]` | — | — | Предварительно вычисленные уровни waveform. |
148
149
  | `preloadedBlob` | `Blob \| null` | — | — | Blob для мгновенного воспроизведения без сетевого запроса. |
149
150
  | `onEnded` | `() => void` | — | — | Колбэк окончания воспроизведения. |
@@ -153,6 +154,7 @@ const Messages = () => {
153
154
  **Ref API (`VoiceMessagePlayerRef`):**
154
155
 
155
156
  - `play()` — программный запуск воспроизведения.
157
+ - `prefetch()` — загрузка аудио без воспроизведения (fetch + decode waveform).
156
158
 
157
159
  #### `VoicePlaybackChain`
158
160
 
@@ -5,6 +5,7 @@
5
5
  */
6
6
  export interface VoiceMessagePlayerRef {
7
7
  play: () => void;
8
+ prefetch: () => void;
8
9
  }
9
10
  declare const VoiceMessagePlayer: any;
10
11
  export { VoiceMessagePlayer };
@@ -1 +1 @@
1
- {"version":3,"file":"VoiceMessagePlayer.d.ts","sourceRoot":"","sources":["../../../src/components/VoiceMessagePlayer/VoiceMessagePlayer.tsx"],"names":[],"mappings":"AAAA;;;;GAIG;AA0BH,MAAM,WAAW,qBAAqB;IAClC,IAAI,EAAE,MAAM,IAAI,CAAC;CACpB;AAcD,QAAA,MAAM,kBAAkB,KAmJtB,CAAC;AAGH,OAAO,EAAE,kBAAkB,EAAE,CAAC"}
1
+ {"version":3,"file":"VoiceMessagePlayer.d.ts","sourceRoot":"","sources":["../../../src/components/VoiceMessagePlayer/VoiceMessagePlayer.tsx"],"names":[],"mappings":"AAAA;;;;GAIG;AA0BH,MAAM,WAAW,qBAAqB;IAClC,IAAI,EAAE,MAAM,IAAI,CAAC;IACjB,QAAQ,EAAE,MAAM,IAAI,CAAC;CACxB;AAeD,QAAA,MAAM,kBAAkB,KAsLtB,CAAC;AAGH,OAAO,EAAE,kBAAkB,EAAE,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"useVoiceMessagePlayer.d.ts","sourceRoot":"","sources":["../../src/hooks/useVoiceMessagePlayer.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AASH,OAAO,KAAK,EAAE,4BAA4B,EAAE,2BAA2B,EAAE,MAAM,UAAU,CAAC;AAG1F,QAAA,MAAM,qBAAqB,GAAI,SAAS,4BAA4B,KAAG,2BAmJtE,CAAC;AAGF,OAAO,EAAE,qBAAqB,EAAE,CAAC"}
1
+ {"version":3,"file":"useVoiceMessagePlayer.d.ts","sourceRoot":"","sources":["../../src/hooks/useVoiceMessagePlayer.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AASH,OAAO,KAAK,EAAE,4BAA4B,EAAE,2BAA2B,EAAE,MAAM,UAAU,CAAC;AAG1F,QAAA,MAAM,qBAAqB,GAAI,SAAS,4BAA4B,KAAG,2BA6JtE,CAAC;AAGF,OAAO,EAAE,qBAAqB,EAAE,CAAC"}
package/dist/index.cjs CHANGED
@@ -962,6 +962,13 @@ const useVoiceMessagePlayer = (options) => {
962
962
  },
963
963
  },
964
964
  });
965
+ /** Pre-fetch audio without playing (for viewport preloading) */
966
+ const prefetch = React.useCallback(() => {
967
+ if (isLoading || audioRef.current) {
968
+ return;
969
+ }
970
+ void ensureAudioLoaded();
971
+ }, [isLoading, ensureAudioLoaded]);
965
972
  /** Load audio and auto-play after loaded */
966
973
  const preload = React.useCallback(() => {
967
974
  if (isLoading || audioRef.current) {
@@ -1032,6 +1039,7 @@ const useVoiceMessagePlayer = (options) => {
1032
1039
  currentTime,
1033
1040
  waveformLevels,
1034
1041
  seekTo,
1042
+ prefetch,
1035
1043
  preload,
1036
1044
  formatTime,
1037
1045
  togglePlayback,
@@ -2130,8 +2138,9 @@ const BAR_GAP = 2;
2130
2138
  const BAR_MIN_HEIGHT = 2;
2131
2139
  const BAR_MAX_HEIGHT = 24;
2132
2140
  const VoiceMessagePlayer = React.forwardRef((props, ref) => {
2133
- const { src, onClick, onEnded, chainId, duration, className, waveformData, } = props;
2141
+ const { src, onClick, onEnded, chainId, duration, className, waveformData, autoPreload = true, } = props;
2134
2142
  const trackRef = React.useRef(null);
2143
+ const wrapperRef = React.useRef(null);
2135
2144
  const [trackWidth, setTrackWidth] = React.useState(0);
2136
2145
  const handleEnded = React.useCallback(() => {
2137
2146
  onEnded === null || onEnded === void 0 ? void 0 : onEnded();
@@ -2139,14 +2148,37 @@ const VoiceMessagePlayer = React.forwardRef((props, ref) => {
2139
2148
  VoicePlaybackChain.playNext(chainId);
2140
2149
  }
2141
2150
  }, [onEnded, chainId]);
2142
- const { play, progress, isLoaded, isPlaying, isLoading, currentTime, waveformLevels, seekTo, preload, formatTime, togglePlayback, duration: resolvedDuration, } = useVoiceMessagePlayer({
2151
+ const { play, progress, isLoaded, isPlaying, isLoading, currentTime, waveformLevels, seekTo, prefetch, preload, formatTime, togglePlayback, duration: resolvedDuration, } = useVoiceMessagePlayer({
2143
2152
  src,
2144
2153
  duration,
2145
2154
  waveformData,
2146
2155
  preloadedBlob: props.preloadedBlob,
2147
2156
  onEnded: handleEnded,
2148
2157
  });
2149
- React.useImperativeHandle(ref, () => ({ play }), [play]);
2158
+ React.useImperativeHandle(ref, () => ({ play, prefetch }), [play, prefetch]);
2159
+ /** Auto-preload audio when player enters viewport */
2160
+ React.useEffect(() => {
2161
+ if (!autoPreload || isLoaded || isLoading) {
2162
+ return;
2163
+ }
2164
+ const el = wrapperRef.current;
2165
+ if (!el) {
2166
+ return;
2167
+ }
2168
+ const observer = new IntersectionObserver(([entry]) => {
2169
+ if (entry.isIntersecting) {
2170
+ prefetch();
2171
+ observer.disconnect();
2172
+ }
2173
+ }, { threshold: 0.1 });
2174
+ observer.observe(el);
2175
+ return () => observer.disconnect();
2176
+ }, [
2177
+ isLoaded,
2178
+ prefetch,
2179
+ isLoading,
2180
+ autoPreload,
2181
+ ]);
2150
2182
  /** Register in playback chain for sequential playback */
2151
2183
  React.useEffect(() => {
2152
2184
  if (chainId != null) {
@@ -2193,7 +2225,7 @@ const VoiceMessagePlayer = React.forwardRef((props, ref) => {
2193
2225
  const peak = Math.max(...raw, 1);
2194
2226
  return raw.map((v) => (v / peak) * 100);
2195
2227
  }, [waveformLevels, trackWidth]);
2196
- return (jsxRuntime.jsxs(PlayerWrapper, { onClick: onClick, className: className, children: [jsxRuntime.jsx(PlayButton, { type: "button", disabled: isLoading || (!src && !props.preloadedBlob), onClick: isLoaded ? togglePlayback : preload, "aria-label": isPlaying ? 'Pause' : 'Play', children: isLoading ? (jsxRuntime.jsx(LoadingSpinner, {})) : isPlaying ? (jsxRuntime.jsx(icons.Icon24Pause, {})) : (jsxRuntime.jsx(icons.Icon24Play, {})) }), jsxRuntime.jsxs(ContentColumn, { children: [jsxRuntime.jsx(WaveformTrack, { ref: trackRef, onClick: handleTrackClick, children: interpolatedBars.map((level, i) => {
2228
+ return (jsxRuntime.jsxs(PlayerWrapper, { ref: wrapperRef, onClick: onClick, className: className, children: [jsxRuntime.jsx(PlayButton, { type: "button", disabled: isLoading || (!src && !props.preloadedBlob), onClick: isLoaded ? togglePlayback : preload, "aria-label": isPlaying ? 'Pause' : 'Play', children: isLoading ? (jsxRuntime.jsx(LoadingSpinner, {})) : isPlaying ? (jsxRuntime.jsx(icons.Icon24Pause, {})) : (jsxRuntime.jsx(icons.Icon24Play, {})) }), jsxRuntime.jsxs(ContentColumn, { children: [jsxRuntime.jsx(WaveformTrack, { ref: trackRef, onClick: handleTrackClick, children: interpolatedBars.map((level, i) => {
2197
2229
  const normalized = Math.min(100, level);
2198
2230
  const height = BAR_MIN_HEIGHT + (normalized / 100) * (BAR_MAX_HEIGHT - BAR_MIN_HEIGHT);
2199
2231
  const barProgress = interpolatedBars.length > 0