Skip to content

Commit 6a7ee27

Browse files
committed
fix: 修复部分歌词滚动异常
1 parent 041990d commit 6a7ee27

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/utils/lyricFormat.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
const lyricFormat = (lrc) => {
77
// 匹配时间轴和歌词文本的正则表达式
8-
const regex = /^\[(.*?)\]\s*(.+?)\s*$/;
8+
const regex = /^\[([^\]]+)\]\s*(.+?)\s*$/;
99
// 将歌词字符串按行分割为数组
1010
const lines = lrc.split("\n");
1111
// 对每一行进行转换
@@ -15,9 +15,11 @@ const lyricFormat = (lrc) => {
1515
// 转换时间轴和歌词文本为对象
1616
.map((line) => {
1717
const [, time, text] = line.match(regex);
18-
const seconds = time
19-
.split(":")
20-
.reduce((acc, cur) => acc * 60 + parseFloat(cur), 0);
18+
const parts = time.split(":");
19+
let seconds = parseInt(parts[0]) * 60 + parseFloat(parts[1]);
20+
if (parts.length > 2) {
21+
seconds += parseFloat(parts[2]) / 1000;
22+
}
2123
return { time: seconds, lyric: text.trim() };
2224
}).filter((element) => element.lyric.trim() !== "");
2325
// 检查是否为纯音乐,是则返回空数组
@@ -28,4 +30,5 @@ const lyricFormat = (lrc) => {
2830
return result;
2931
};
3032

33+
3134
export default lyricFormat;

0 commit comments

Comments
 (0)