lrc-audio-player 0.1.3 → 0.1.4

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/dist/react.cjs CHANGED
@@ -403,32 +403,69 @@ function useLyricPlayer(options) {
403
403
  const [currentLine, setCurrentLine] = (0, import_react.useState)(null);
404
404
  const [currentIndex, setCurrentIndex] = (0, import_react.useState)(-1);
405
405
  const [lines, setLines] = (0, import_react.useState)([]);
406
+ const [isLoading, setIsLoading] = (0, import_react.useState)(false);
407
+ const [error, setError] = (0, import_react.useState)(null);
406
408
  const optionsRef = (0, import_react.useRef)(options);
407
409
  optionsRef.current = options;
408
410
  (0, import_react.useEffect)(() => {
409
411
  const audioEl = audioRef.current;
410
412
  if (!audioEl) return;
411
- const { audio, lyrics, offsetMs } = optionsRef.current;
413
+ const { audio, lyrics, offsetMs, skipCBR, cbrBitrate } = optionsRef.current;
412
414
  if (audio) audioEl.src = audio;
413
- const instance = new LyricPlayer({
414
- audio: audioEl,
415
- lyrics,
416
- offsetMs
417
- });
418
- setPlayer(instance);
419
- setLines(instance.lines);
415
+ let cancelled = false;
416
+ setIsLoading(true);
417
+ setError(null);
418
+ setPlayer(null);
419
+ setLines([]);
420
420
  setCurrentLine(null);
421
421
  setCurrentIndex(-1);
422
- instance.on("linechange", (line, index) => {
423
- setCurrentLine(line);
424
- setCurrentIndex(index);
422
+ LyricPlayer.create({
423
+ audio: audioEl,
424
+ lyrics,
425
+ offsetMs,
426
+ skipCBR,
427
+ cbrBitrate
428
+ }).then((instance) => {
429
+ if (cancelled) {
430
+ instance.destroy();
431
+ return;
432
+ }
433
+ setPlayer(instance);
434
+ setLines(instance.lines);
435
+ setIsLoading(false);
436
+ instance.on("linechange", (line, index) => {
437
+ setCurrentLine(line);
438
+ setCurrentIndex(index);
439
+ });
440
+ }).catch((err) => {
441
+ if (!cancelled) {
442
+ setError(err instanceof Error ? err : new Error(String(err)));
443
+ setIsLoading(false);
444
+ }
425
445
  });
426
446
  return () => {
427
- instance.destroy();
428
- setPlayer(null);
447
+ cancelled = true;
448
+ setPlayer((prev) => {
449
+ prev?.destroy();
450
+ return null;
451
+ });
429
452
  };
430
- }, [options.audio, options.lyrics, options.offsetMs]);
431
- return { player, audioRef, currentLine, currentIndex, lines };
453
+ }, [
454
+ options.audio,
455
+ options.lyrics,
456
+ options.offsetMs,
457
+ options.skipCBR,
458
+ options.cbrBitrate
459
+ ]);
460
+ return {
461
+ player,
462
+ audioRef,
463
+ currentLine,
464
+ currentIndex,
465
+ lines,
466
+ isLoading,
467
+ error
468
+ };
432
469
  }
433
470
  // Annotate the CommonJS export names for ESM import in node:
434
471
  0 && (module.exports = {
package/dist/react.d.cts CHANGED
@@ -4,6 +4,10 @@ export { f as LyricSource } from './player-sLsiwLVO.cjs';
4
4
  interface UseLyricPlayerOptions extends Omit<LyricPlayerOptions, "audio"> {
5
5
  /** Audio source URL. Set on the bound <audio> element. */
6
6
  audio?: string;
7
+ /** Skip CBR conversion if your audio is already constant bitrate. */
8
+ skipCBR?: boolean;
9
+ /** Target bitrate for CBR conversion. @default '128k' */
10
+ cbrBitrate?: string;
7
11
  }
8
12
  interface UseLyricPlayerResult {
9
13
  /** The underlying player instance (null until mounted and ready). */
@@ -16,6 +20,10 @@ interface UseLyricPlayerResult {
16
20
  currentIndex: number;
17
21
  /** All parsed lyric lines (empty until lyrics are loaded). */
18
22
  lines: LyricLine[];
23
+ /** Whether the player is initializing (CBR conversion in progress). */
24
+ isLoading: boolean;
25
+ /** Error if initialization failed. */
26
+ error: Error | null;
19
27
  }
20
28
  /**
21
29
  * React hook that creates a {@link LyricPlayer} bound to an `<audio>`
@@ -28,7 +36,7 @@ interface UseLyricPlayerResult {
28
36
  * ```tsx
29
37
  * 'use client';
30
38
  *
31
- * const { audioRef, currentLine, lines, player } = useLyricPlayer({
39
+ * const { audioRef, currentLine, lines, player, isLoading } = useLyricPlayer({
32
40
  * audio: '/song.mp3',
33
41
  * lyrics: lrcText,
34
42
  * });
@@ -36,15 +44,11 @@ interface UseLyricPlayerResult {
36
44
  * return (
37
45
  * <>
38
46
  * <audio ref={audioRef} controls />
47
+ * {isLoading && <p>Loading audio…</p>}
39
48
  * <p>{currentLine?.text}</p>
40
49
  * </>
41
50
  * );
42
51
  * ```
43
- *
44
- * The player is recreated whenever `audio` or `lyrics` change. If you're
45
- * fetching lyrics asynchronously, wait until they're loaded before calling
46
- * this hook (or pass `lyrics: ''` while loading - an empty string parses
47
- * to zero lines and is cheap to recreate).
48
52
  */
49
53
  declare function useLyricPlayer(options: UseLyricPlayerOptions): UseLyricPlayerResult;
50
54
 
package/dist/react.d.ts CHANGED
@@ -4,6 +4,10 @@ export { f as LyricSource } from './player-sLsiwLVO.js';
4
4
  interface UseLyricPlayerOptions extends Omit<LyricPlayerOptions, "audio"> {
5
5
  /** Audio source URL. Set on the bound <audio> element. */
6
6
  audio?: string;
7
+ /** Skip CBR conversion if your audio is already constant bitrate. */
8
+ skipCBR?: boolean;
9
+ /** Target bitrate for CBR conversion. @default '128k' */
10
+ cbrBitrate?: string;
7
11
  }
8
12
  interface UseLyricPlayerResult {
9
13
  /** The underlying player instance (null until mounted and ready). */
@@ -16,6 +20,10 @@ interface UseLyricPlayerResult {
16
20
  currentIndex: number;
17
21
  /** All parsed lyric lines (empty until lyrics are loaded). */
18
22
  lines: LyricLine[];
23
+ /** Whether the player is initializing (CBR conversion in progress). */
24
+ isLoading: boolean;
25
+ /** Error if initialization failed. */
26
+ error: Error | null;
19
27
  }
20
28
  /**
21
29
  * React hook that creates a {@link LyricPlayer} bound to an `<audio>`
@@ -28,7 +36,7 @@ interface UseLyricPlayerResult {
28
36
  * ```tsx
29
37
  * 'use client';
30
38
  *
31
- * const { audioRef, currentLine, lines, player } = useLyricPlayer({
39
+ * const { audioRef, currentLine, lines, player, isLoading } = useLyricPlayer({
32
40
  * audio: '/song.mp3',
33
41
  * lyrics: lrcText,
34
42
  * });
@@ -36,15 +44,11 @@ interface UseLyricPlayerResult {
36
44
  * return (
37
45
  * <>
38
46
  * <audio ref={audioRef} controls />
47
+ * {isLoading && <p>Loading audio…</p>}
39
48
  * <p>{currentLine?.text}</p>
40
49
  * </>
41
50
  * );
42
51
  * ```
43
- *
44
- * The player is recreated whenever `audio` or `lyrics` change. If you're
45
- * fetching lyrics asynchronously, wait until they're loaded before calling
46
- * this hook (or pass `lyrics: ''` while loading - an empty string parses
47
- * to zero lines and is cheap to recreate).
48
52
  */
49
53
  declare function useLyricPlayer(options: UseLyricPlayerOptions): UseLyricPlayerResult;
50
54
 
package/dist/react.js CHANGED
@@ -11,32 +11,69 @@ function useLyricPlayer(options) {
11
11
  const [currentLine, setCurrentLine] = useState(null);
12
12
  const [currentIndex, setCurrentIndex] = useState(-1);
13
13
  const [lines, setLines] = useState([]);
14
+ const [isLoading, setIsLoading] = useState(false);
15
+ const [error, setError] = useState(null);
14
16
  const optionsRef = useRef(options);
15
17
  optionsRef.current = options;
16
18
  useEffect(() => {
17
19
  const audioEl = audioRef.current;
18
20
  if (!audioEl) return;
19
- const { audio, lyrics, offsetMs } = optionsRef.current;
21
+ const { audio, lyrics, offsetMs, skipCBR, cbrBitrate } = optionsRef.current;
20
22
  if (audio) audioEl.src = audio;
21
- const instance = new LyricPlayer({
22
- audio: audioEl,
23
- lyrics,
24
- offsetMs
25
- });
26
- setPlayer(instance);
27
- setLines(instance.lines);
23
+ let cancelled = false;
24
+ setIsLoading(true);
25
+ setError(null);
26
+ setPlayer(null);
27
+ setLines([]);
28
28
  setCurrentLine(null);
29
29
  setCurrentIndex(-1);
30
- instance.on("linechange", (line, index) => {
31
- setCurrentLine(line);
32
- setCurrentIndex(index);
30
+ LyricPlayer.create({
31
+ audio: audioEl,
32
+ lyrics,
33
+ offsetMs,
34
+ skipCBR,
35
+ cbrBitrate
36
+ }).then((instance) => {
37
+ if (cancelled) {
38
+ instance.destroy();
39
+ return;
40
+ }
41
+ setPlayer(instance);
42
+ setLines(instance.lines);
43
+ setIsLoading(false);
44
+ instance.on("linechange", (line, index) => {
45
+ setCurrentLine(line);
46
+ setCurrentIndex(index);
47
+ });
48
+ }).catch((err) => {
49
+ if (!cancelled) {
50
+ setError(err instanceof Error ? err : new Error(String(err)));
51
+ setIsLoading(false);
52
+ }
33
53
  });
34
54
  return () => {
35
- instance.destroy();
36
- setPlayer(null);
55
+ cancelled = true;
56
+ setPlayer((prev) => {
57
+ prev?.destroy();
58
+ return null;
59
+ });
37
60
  };
38
- }, [options.audio, options.lyrics, options.offsetMs]);
39
- return { player, audioRef, currentLine, currentIndex, lines };
61
+ }, [
62
+ options.audio,
63
+ options.lyrics,
64
+ options.offsetMs,
65
+ options.skipCBR,
66
+ options.cbrBitrate
67
+ ]);
68
+ return {
69
+ player,
70
+ audioRef,
71
+ currentLine,
72
+ currentIndex,
73
+ lines,
74
+ isLoading,
75
+ error
76
+ };
40
77
  }
41
78
  export {
42
79
  useLyricPlayer
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lrc-audio-player",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "Sync LRC/word-level lyrics to an HTML audio element with one constructor. Includes an optional React hook at lrc-audio-player/react.",
5
5
  "repository": {
6
6
  "type": "git",