lrc-audio-player 0.1.3 → 0.1.5
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 +98 -15
- package/dist/react.d.cts +29 -7
- package/dist/react.d.ts +29 -7
- package/dist/react.js +99 -16
- package/package.json +1 -1
package/dist/react.cjs
CHANGED
|
@@ -402,33 +402,116 @@ function useLyricPlayer(options) {
|
|
|
402
402
|
const [player, setPlayer] = (0, import_react.useState)(null);
|
|
403
403
|
const [currentLine, setCurrentLine] = (0, import_react.useState)(null);
|
|
404
404
|
const [currentIndex, setCurrentIndex] = (0, import_react.useState)(-1);
|
|
405
|
+
const [currentToken, setCurrentToken] = (0, import_react.useState)(null);
|
|
405
406
|
const [lines, setLines] = (0, import_react.useState)([]);
|
|
407
|
+
const [isLoading, setIsLoading] = (0, import_react.useState)(false);
|
|
408
|
+
const [error, setError] = (0, import_react.useState)(null);
|
|
409
|
+
const [isPlaying, setIsPlaying] = (0, import_react.useState)(false);
|
|
410
|
+
const [currentTime, setCurrentTime] = (0, import_react.useState)(0);
|
|
411
|
+
const [duration, setDuration] = (0, import_react.useState)(0);
|
|
406
412
|
const optionsRef = (0, import_react.useRef)(options);
|
|
407
413
|
optionsRef.current = options;
|
|
408
414
|
(0, import_react.useEffect)(() => {
|
|
409
415
|
const audioEl = audioRef.current;
|
|
410
416
|
if (!audioEl) return;
|
|
411
|
-
const { audio, lyrics, offsetMs } = optionsRef.current;
|
|
417
|
+
const { audio, lyrics, offsetMs, skipCBR, cbrBitrate } = optionsRef.current;
|
|
412
418
|
if (audio) audioEl.src = audio;
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
setPlayer(instance);
|
|
419
|
-
setLines(instance.lines);
|
|
419
|
+
let cancelled = false;
|
|
420
|
+
setIsLoading(true);
|
|
421
|
+
setError(null);
|
|
422
|
+
setPlayer(null);
|
|
423
|
+
setLines([]);
|
|
420
424
|
setCurrentLine(null);
|
|
421
425
|
setCurrentIndex(-1);
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
426
|
+
setCurrentToken(null);
|
|
427
|
+
setIsPlaying(false);
|
|
428
|
+
setCurrentTime(0);
|
|
429
|
+
setDuration(0);
|
|
430
|
+
LyricPlayer.create({
|
|
431
|
+
audio: audioEl,
|
|
432
|
+
lyrics,
|
|
433
|
+
offsetMs,
|
|
434
|
+
skipCBR,
|
|
435
|
+
cbrBitrate
|
|
436
|
+
}).then((instance) => {
|
|
437
|
+
if (cancelled) {
|
|
438
|
+
instance.destroy();
|
|
439
|
+
return;
|
|
440
|
+
}
|
|
441
|
+
setPlayer(instance);
|
|
442
|
+
setLines(instance.lines);
|
|
443
|
+
setDuration(instance.duration);
|
|
444
|
+
setIsLoading(false);
|
|
445
|
+
instance.on("linechange", (line, index) => {
|
|
446
|
+
setCurrentLine(line);
|
|
447
|
+
setCurrentIndex(index);
|
|
448
|
+
});
|
|
449
|
+
instance.on("timeupdate", (time) => {
|
|
450
|
+
setCurrentTime(time);
|
|
451
|
+
setCurrentToken(instance.getCurrentToken());
|
|
452
|
+
});
|
|
453
|
+
instance.on("play", () => setIsPlaying(true));
|
|
454
|
+
instance.on("pause", () => setIsPlaying(false));
|
|
455
|
+
instance.on("ended", () => setIsPlaying(false));
|
|
456
|
+
}).catch((err) => {
|
|
457
|
+
if (!cancelled) {
|
|
458
|
+
setError(err instanceof Error ? err : new Error(String(err)));
|
|
459
|
+
setIsLoading(false);
|
|
460
|
+
}
|
|
425
461
|
});
|
|
426
462
|
return () => {
|
|
427
|
-
|
|
428
|
-
setPlayer(
|
|
463
|
+
cancelled = true;
|
|
464
|
+
setPlayer((prev) => {
|
|
465
|
+
prev?.destroy();
|
|
466
|
+
return null;
|
|
467
|
+
});
|
|
429
468
|
};
|
|
430
|
-
}, [
|
|
431
|
-
|
|
469
|
+
}, [
|
|
470
|
+
options.audio,
|
|
471
|
+
options.lyrics,
|
|
472
|
+
options.offsetMs,
|
|
473
|
+
options.skipCBR,
|
|
474
|
+
options.cbrBitrate
|
|
475
|
+
]);
|
|
476
|
+
const seek = (0, import_react.useCallback)(
|
|
477
|
+
(timeSeconds) => {
|
|
478
|
+
player?.seek(timeSeconds);
|
|
479
|
+
},
|
|
480
|
+
[player]
|
|
481
|
+
);
|
|
482
|
+
const seekToLine = (0, import_react.useCallback)(
|
|
483
|
+
(index) => {
|
|
484
|
+
player?.seekToLine(index);
|
|
485
|
+
},
|
|
486
|
+
[player]
|
|
487
|
+
);
|
|
488
|
+
const play = (0, import_react.useCallback)(() => {
|
|
489
|
+
return player?.play() ?? Promise.resolve();
|
|
490
|
+
}, [player]);
|
|
491
|
+
const pause = (0, import_react.useCallback)(() => {
|
|
492
|
+
player?.pause();
|
|
493
|
+
}, [player]);
|
|
494
|
+
const toggle = (0, import_react.useCallback)(() => {
|
|
495
|
+
return player?.toggle();
|
|
496
|
+
}, [player]);
|
|
497
|
+
return {
|
|
498
|
+
player,
|
|
499
|
+
audioRef,
|
|
500
|
+
currentLine,
|
|
501
|
+
currentIndex,
|
|
502
|
+
currentToken,
|
|
503
|
+
lines,
|
|
504
|
+
isLoading,
|
|
505
|
+
error,
|
|
506
|
+
isPlaying,
|
|
507
|
+
currentTime,
|
|
508
|
+
duration,
|
|
509
|
+
seek,
|
|
510
|
+
seekToLine,
|
|
511
|
+
play,
|
|
512
|
+
pause,
|
|
513
|
+
toggle
|
|
514
|
+
};
|
|
432
515
|
}
|
|
433
516
|
// Annotate the CommonJS export names for ESM import in node:
|
|
434
517
|
0 && (module.exports = {
|
package/dist/react.d.cts
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
import { h as LyricPlayerOptions, b as LyricPlayer, L as LyricLine } from './player-sLsiwLVO.cjs';
|
|
1
|
+
import { h as LyricPlayerOptions, b as LyricPlayer, L as LyricLine, g as LyricToken } from './player-sLsiwLVO.cjs';
|
|
2
2
|
export { f as LyricSource } from './player-sLsiwLVO.cjs';
|
|
3
3
|
|
|
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). */
|
|
@@ -14,8 +18,30 @@ interface UseLyricPlayerResult {
|
|
|
14
18
|
currentLine: LyricLine | null;
|
|
15
19
|
/** Index of the currently active lyric line (-1 if none yet). */
|
|
16
20
|
currentIndex: number;
|
|
21
|
+
/** The currently active word/token (for word-level LRC), or null. */
|
|
22
|
+
currentToken: LyricToken | null;
|
|
17
23
|
/** All parsed lyric lines (empty until lyrics are loaded). */
|
|
18
24
|
lines: LyricLine[];
|
|
25
|
+
/** Whether the player is initializing (CBR conversion in progress). */
|
|
26
|
+
isLoading: boolean;
|
|
27
|
+
/** Error if initialization failed. */
|
|
28
|
+
error: Error | null;
|
|
29
|
+
/** Whether audio is currently playing. */
|
|
30
|
+
isPlaying: boolean;
|
|
31
|
+
/** Current playback time in seconds. */
|
|
32
|
+
currentTime: number;
|
|
33
|
+
/** Total audio duration in seconds (0 if unknown). */
|
|
34
|
+
duration: number;
|
|
35
|
+
/** Jump to a specific time in seconds. */
|
|
36
|
+
seek: (timeSeconds: number) => void;
|
|
37
|
+
/** Jump to the start of a specific lyric line. */
|
|
38
|
+
seekToLine: (index: number) => void;
|
|
39
|
+
/** Play the audio. */
|
|
40
|
+
play: () => Promise<void>;
|
|
41
|
+
/** Pause the audio. */
|
|
42
|
+
pause: () => void;
|
|
43
|
+
/** Toggle play/pause. */
|
|
44
|
+
toggle: () => Promise<void> | void;
|
|
19
45
|
}
|
|
20
46
|
/**
|
|
21
47
|
* React hook that creates a {@link LyricPlayer} bound to an `<audio>`
|
|
@@ -28,7 +54,7 @@ interface UseLyricPlayerResult {
|
|
|
28
54
|
* ```tsx
|
|
29
55
|
* 'use client';
|
|
30
56
|
*
|
|
31
|
-
* const { audioRef, currentLine, lines,
|
|
57
|
+
* const { audioRef, currentLine, lines, isLoading, isPlaying, currentTime } = useLyricPlayer({
|
|
32
58
|
* audio: '/song.mp3',
|
|
33
59
|
* lyrics: lrcText,
|
|
34
60
|
* });
|
|
@@ -36,15 +62,11 @@ interface UseLyricPlayerResult {
|
|
|
36
62
|
* return (
|
|
37
63
|
* <>
|
|
38
64
|
* <audio ref={audioRef} controls />
|
|
65
|
+
* {isLoading && <p>Loading audio…</p>}
|
|
39
66
|
* <p>{currentLine?.text}</p>
|
|
40
67
|
* </>
|
|
41
68
|
* );
|
|
42
69
|
* ```
|
|
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
70
|
*/
|
|
49
71
|
declare function useLyricPlayer(options: UseLyricPlayerOptions): UseLyricPlayerResult;
|
|
50
72
|
|
package/dist/react.d.ts
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
import { h as LyricPlayerOptions, b as LyricPlayer, L as LyricLine } from './player-sLsiwLVO.js';
|
|
1
|
+
import { h as LyricPlayerOptions, b as LyricPlayer, L as LyricLine, g as LyricToken } from './player-sLsiwLVO.js';
|
|
2
2
|
export { f as LyricSource } from './player-sLsiwLVO.js';
|
|
3
3
|
|
|
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). */
|
|
@@ -14,8 +18,30 @@ interface UseLyricPlayerResult {
|
|
|
14
18
|
currentLine: LyricLine | null;
|
|
15
19
|
/** Index of the currently active lyric line (-1 if none yet). */
|
|
16
20
|
currentIndex: number;
|
|
21
|
+
/** The currently active word/token (for word-level LRC), or null. */
|
|
22
|
+
currentToken: LyricToken | null;
|
|
17
23
|
/** All parsed lyric lines (empty until lyrics are loaded). */
|
|
18
24
|
lines: LyricLine[];
|
|
25
|
+
/** Whether the player is initializing (CBR conversion in progress). */
|
|
26
|
+
isLoading: boolean;
|
|
27
|
+
/** Error if initialization failed. */
|
|
28
|
+
error: Error | null;
|
|
29
|
+
/** Whether audio is currently playing. */
|
|
30
|
+
isPlaying: boolean;
|
|
31
|
+
/** Current playback time in seconds. */
|
|
32
|
+
currentTime: number;
|
|
33
|
+
/** Total audio duration in seconds (0 if unknown). */
|
|
34
|
+
duration: number;
|
|
35
|
+
/** Jump to a specific time in seconds. */
|
|
36
|
+
seek: (timeSeconds: number) => void;
|
|
37
|
+
/** Jump to the start of a specific lyric line. */
|
|
38
|
+
seekToLine: (index: number) => void;
|
|
39
|
+
/** Play the audio. */
|
|
40
|
+
play: () => Promise<void>;
|
|
41
|
+
/** Pause the audio. */
|
|
42
|
+
pause: () => void;
|
|
43
|
+
/** Toggle play/pause. */
|
|
44
|
+
toggle: () => Promise<void> | void;
|
|
19
45
|
}
|
|
20
46
|
/**
|
|
21
47
|
* React hook that creates a {@link LyricPlayer} bound to an `<audio>`
|
|
@@ -28,7 +54,7 @@ interface UseLyricPlayerResult {
|
|
|
28
54
|
* ```tsx
|
|
29
55
|
* 'use client';
|
|
30
56
|
*
|
|
31
|
-
* const { audioRef, currentLine, lines,
|
|
57
|
+
* const { audioRef, currentLine, lines, isLoading, isPlaying, currentTime } = useLyricPlayer({
|
|
32
58
|
* audio: '/song.mp3',
|
|
33
59
|
* lyrics: lrcText,
|
|
34
60
|
* });
|
|
@@ -36,15 +62,11 @@ interface UseLyricPlayerResult {
|
|
|
36
62
|
* return (
|
|
37
63
|
* <>
|
|
38
64
|
* <audio ref={audioRef} controls />
|
|
65
|
+
* {isLoading && <p>Loading audio…</p>}
|
|
39
66
|
* <p>{currentLine?.text}</p>
|
|
40
67
|
* </>
|
|
41
68
|
* );
|
|
42
69
|
* ```
|
|
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
70
|
*/
|
|
49
71
|
declare function useLyricPlayer(options: UseLyricPlayerOptions): UseLyricPlayerResult;
|
|
50
72
|
|
package/dist/react.js
CHANGED
|
@@ -4,39 +4,122 @@ import {
|
|
|
4
4
|
} from "./chunk-3A2M5KTA.js";
|
|
5
5
|
|
|
6
6
|
// src/react.ts
|
|
7
|
-
import { useEffect, useRef, useState } from "react";
|
|
7
|
+
import { useEffect, useRef, useState, useCallback } from "react";
|
|
8
8
|
function useLyricPlayer(options) {
|
|
9
9
|
const audioRef = useRef(null);
|
|
10
10
|
const [player, setPlayer] = useState(null);
|
|
11
11
|
const [currentLine, setCurrentLine] = useState(null);
|
|
12
12
|
const [currentIndex, setCurrentIndex] = useState(-1);
|
|
13
|
+
const [currentToken, setCurrentToken] = useState(null);
|
|
13
14
|
const [lines, setLines] = useState([]);
|
|
15
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
16
|
+
const [error, setError] = useState(null);
|
|
17
|
+
const [isPlaying, setIsPlaying] = useState(false);
|
|
18
|
+
const [currentTime, setCurrentTime] = useState(0);
|
|
19
|
+
const [duration, setDuration] = useState(0);
|
|
14
20
|
const optionsRef = useRef(options);
|
|
15
21
|
optionsRef.current = options;
|
|
16
22
|
useEffect(() => {
|
|
17
23
|
const audioEl = audioRef.current;
|
|
18
24
|
if (!audioEl) return;
|
|
19
|
-
const { audio, lyrics, offsetMs } = optionsRef.current;
|
|
25
|
+
const { audio, lyrics, offsetMs, skipCBR, cbrBitrate } = optionsRef.current;
|
|
20
26
|
if (audio) audioEl.src = audio;
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
setPlayer(instance);
|
|
27
|
-
setLines(instance.lines);
|
|
27
|
+
let cancelled = false;
|
|
28
|
+
setIsLoading(true);
|
|
29
|
+
setError(null);
|
|
30
|
+
setPlayer(null);
|
|
31
|
+
setLines([]);
|
|
28
32
|
setCurrentLine(null);
|
|
29
33
|
setCurrentIndex(-1);
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
34
|
+
setCurrentToken(null);
|
|
35
|
+
setIsPlaying(false);
|
|
36
|
+
setCurrentTime(0);
|
|
37
|
+
setDuration(0);
|
|
38
|
+
LyricPlayer.create({
|
|
39
|
+
audio: audioEl,
|
|
40
|
+
lyrics,
|
|
41
|
+
offsetMs,
|
|
42
|
+
skipCBR,
|
|
43
|
+
cbrBitrate
|
|
44
|
+
}).then((instance) => {
|
|
45
|
+
if (cancelled) {
|
|
46
|
+
instance.destroy();
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
setPlayer(instance);
|
|
50
|
+
setLines(instance.lines);
|
|
51
|
+
setDuration(instance.duration);
|
|
52
|
+
setIsLoading(false);
|
|
53
|
+
instance.on("linechange", (line, index) => {
|
|
54
|
+
setCurrentLine(line);
|
|
55
|
+
setCurrentIndex(index);
|
|
56
|
+
});
|
|
57
|
+
instance.on("timeupdate", (time) => {
|
|
58
|
+
setCurrentTime(time);
|
|
59
|
+
setCurrentToken(instance.getCurrentToken());
|
|
60
|
+
});
|
|
61
|
+
instance.on("play", () => setIsPlaying(true));
|
|
62
|
+
instance.on("pause", () => setIsPlaying(false));
|
|
63
|
+
instance.on("ended", () => setIsPlaying(false));
|
|
64
|
+
}).catch((err) => {
|
|
65
|
+
if (!cancelled) {
|
|
66
|
+
setError(err instanceof Error ? err : new Error(String(err)));
|
|
67
|
+
setIsLoading(false);
|
|
68
|
+
}
|
|
33
69
|
});
|
|
34
70
|
return () => {
|
|
35
|
-
|
|
36
|
-
setPlayer(
|
|
71
|
+
cancelled = true;
|
|
72
|
+
setPlayer((prev) => {
|
|
73
|
+
prev?.destroy();
|
|
74
|
+
return null;
|
|
75
|
+
});
|
|
37
76
|
};
|
|
38
|
-
}, [
|
|
39
|
-
|
|
77
|
+
}, [
|
|
78
|
+
options.audio,
|
|
79
|
+
options.lyrics,
|
|
80
|
+
options.offsetMs,
|
|
81
|
+
options.skipCBR,
|
|
82
|
+
options.cbrBitrate
|
|
83
|
+
]);
|
|
84
|
+
const seek = useCallback(
|
|
85
|
+
(timeSeconds) => {
|
|
86
|
+
player?.seek(timeSeconds);
|
|
87
|
+
},
|
|
88
|
+
[player]
|
|
89
|
+
);
|
|
90
|
+
const seekToLine = useCallback(
|
|
91
|
+
(index) => {
|
|
92
|
+
player?.seekToLine(index);
|
|
93
|
+
},
|
|
94
|
+
[player]
|
|
95
|
+
);
|
|
96
|
+
const play = useCallback(() => {
|
|
97
|
+
return player?.play() ?? Promise.resolve();
|
|
98
|
+
}, [player]);
|
|
99
|
+
const pause = useCallback(() => {
|
|
100
|
+
player?.pause();
|
|
101
|
+
}, [player]);
|
|
102
|
+
const toggle = useCallback(() => {
|
|
103
|
+
return player?.toggle();
|
|
104
|
+
}, [player]);
|
|
105
|
+
return {
|
|
106
|
+
player,
|
|
107
|
+
audioRef,
|
|
108
|
+
currentLine,
|
|
109
|
+
currentIndex,
|
|
110
|
+
currentToken,
|
|
111
|
+
lines,
|
|
112
|
+
isLoading,
|
|
113
|
+
error,
|
|
114
|
+
isPlaying,
|
|
115
|
+
currentTime,
|
|
116
|
+
duration,
|
|
117
|
+
seek,
|
|
118
|
+
seekToLine,
|
|
119
|
+
play,
|
|
120
|
+
pause,
|
|
121
|
+
toggle
|
|
122
|
+
};
|
|
40
123
|
}
|
|
41
124
|
export {
|
|
42
125
|
useLyricPlayer
|
package/package.json
CHANGED