Skip to content

Commit 3154327

Browse files
committed
core: 尝试改善隐藏歌词行的性能,并优化暂停呈现效果
player: 修正部分代码
1 parent 68f8310 commit 3154327

File tree

6 files changed

+90
-22
lines changed

6 files changed

+90
-22
lines changed

packages/core/src/lyric-player/base.ts

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,9 @@ export abstract class LyricPlayerBase
627627
.reduce(
628628
(acc, el) =>
629629
acc +
630-
(el.getLine().isBG ? 0 : (this.lyricLinesSize.get(el)?.[1] ?? 0)),
630+
(el.getLine().isBG && this.isPlaying
631+
? 0
632+
: (this.lyricLinesSize.get(el)?.[1] ?? 0)),
631633
0,
632634
);
633635
this.scrollBoundary[0] = -scrollOffset;
@@ -673,8 +675,12 @@ export abstract class LyricPlayerBase
673675
let targetOpacity: number;
674676

675677
if (this.hidePassedLines) {
676-
if (i < (interlude ? interlude[2] + 1 : this.scrollToIndex)) {
677-
targetOpacity = 0;
678+
if (
679+
i < (interlude ? interlude[2] + 1 : this.scrollToIndex) &&
680+
this.isPlaying
681+
) {
682+
// 为了避免浏览器优化,这里使用了一个极小但不为零的值(几乎不可见)
683+
targetOpacity = 0.00001;
678684
} else if (hasBuffered) {
679685
targetOpacity = 0.85;
680686
} else {
@@ -706,30 +712,33 @@ export abstract class LyricPlayerBase
706712

707713
const SCALE_ASPECT = this.enableScale ? 97 : 100;
708714

715+
let targetScale = 100;
716+
717+
if (!isActive && this.isPlaying) {
718+
if (line.isBG) {
719+
targetScale = 75;
720+
} else {
721+
targetScale = SCALE_ASPECT;
722+
}
723+
}
724+
709725
lineObj.setTransform(
710726
curPos,
711-
isActive ? 100 : line.isBG ? 75 : SCALE_ASPECT,
727+
targetScale,
712728
targetOpacity,
713729
window.innerWidth <= 1024 ? blurLevel * 0.8 : blurLevel,
714730
force,
715731
delay,
716732
);
717-
if (line.isBG && isActive) {
733+
if (line.isBG && (isActive || !this.isPlaying)) {
718734
curPos += this.lyricLinesSize.get(lineObj)?.[1] ?? 0;
719735
} else if (!line.isBG) {
720736
curPos += this.lyricLinesSize.get(lineObj)?.[1] ?? 0;
721737
}
722738
if (curPos >= 0 && !this.isSeeking) {
723739
if (!line.isBG) delay += baseDelay;
724-
// if (i >= this.scrollToIndex - 1) baseDelay *= 1.05;
725-
// baseDelay = Math.min(baseDelay, 0.055);
726-
727-
// delay += 0.05;
728740

729-
// baseDelay = baseDelay > 0.15 ? 0 : baseDelay;
730-
// delay = (i - this.scrollToIndex) * 0.06;
731741
if (i >= this.scrollToIndex) baseDelay /= 1.05;
732-
// baseDelay = Math.max(baseDelay, 0.04);
733742
}
734743
});
735744
this.scrollBoundary[1] = curPos + this.scrollOffset - this.size[1] / 2;
@@ -781,17 +790,26 @@ export abstract class LyricPlayerBase
781790
}
782791
}
783792
}
793+
protected isPlaying = true;
784794
/**
785-
* 暂停部分效果演出,目前会暂停播放间奏点的动画
795+
* 暂停部分效果演出,目前会暂停播放间奏点的动画,且将背景歌词显示出来
786796
*/
787797
pause() {
788798
this.interludeDots.pause();
799+
if (this.isPlaying) {
800+
this.isPlaying = false;
801+
this.calcLayout();
802+
}
789803
}
790804
/**
791805
* 恢复部分效果演出,目前会恢复播放间奏点的动画
792806
*/
793807
resume() {
794808
this.interludeDots.resume();
809+
if (!this.isPlaying) {
810+
this.isPlaying = true;
811+
this.calcLayout();
812+
}
795813
}
796814
/**
797815
* 更新动画,这个函数应该被逐帧调用或者在以下情况下调用一次:

packages/core/src/lyric-player/dom/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ export class DomLyricPlayer extends LyricPlayerBase {
173173

174174
override pause() {
175175
super.pause();
176+
this.element.classList.remove("playing");
176177
this.interludeDots.pause();
177178
for (const line of this.currentLyricLineObjects) {
178179
line.pause();
@@ -181,6 +182,7 @@ export class DomLyricPlayer extends LyricPlayerBase {
181182

182183
override resume() {
183184
super.resume();
185+
this.element.classList.add("playing");
184186
this.interludeDots.resume();
185187
for (const line of this.currentLyricLineObjects) {
186188
line.resume();

packages/core/src/styles/lyric-player.module.css

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
}
4141

4242
.lyricBgLine {
43-
opacity: 0;
43+
opacity: 0.0001;
4444
font-size: max(calc(1em * var(--amll-lp-bg-line-scale, 0.7)), 10px);
4545
transition: opacity 0.25s, scale 0.5s, filter 0.2s, background-color 0.25s,
4646
box-shadow 0.25s;
@@ -68,6 +68,10 @@
6868
padding-left: 15%;
6969
}
7070
}
71+
72+
&:not(:global(.playing)) > .lyricBgLine {
73+
opacity: 0.4;
74+
}
7175
}
7276

7377
.lyricDuetLine {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const SAFE_FLAG_CHUNK_SIZE = Number.MAX_SAFE_INTEGER.toString(2).length;
2+
3+
export class FlagSets {
4+
private flags: number[] = [];
5+
6+
add(flag: number) {
7+
const chunkIndex = (flag / SAFE_FLAG_CHUNK_SIZE) | 0;
8+
const chunkOffset = flag % SAFE_FLAG_CHUNK_SIZE;
9+
this.flags[chunkIndex] |= 1 << chunkOffset;
10+
}
11+
12+
delete(flag: number) {
13+
const chunkIndex = (flag / SAFE_FLAG_CHUNK_SIZE) | 0;
14+
const chunkOffset = flag % SAFE_FLAG_CHUNK_SIZE;
15+
this.flags[chunkIndex] &= ~(1 << chunkOffset);
16+
}
17+
18+
has(flag: number) {
19+
const chunkIndex = (flag / SAFE_FLAG_CHUNK_SIZE) | 0;
20+
const chunkOffset = flag % SAFE_FLAG_CHUNK_SIZE;
21+
return (this.flags[chunkIndex] & (1 << chunkOffset)) !== 0;
22+
}
23+
}

packages/player/src/components/ExtensionContext/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const SingleExtensionContext: FC<{
5454
}> = ({ extensionMeta, waitForDependency, extPromise }) => {
5555
const store = useStore();
5656
const { i18n } = useTranslation();
57-
const cancelRef = useRef<Notify>();
57+
const cancelRef = useRef<Notify>(undefined);
5858
const setLoadedExtension = useSetAtom(loadedExtensionAtom);
5959
useEffect(() => {
6060
let canceled = false;

packages/player/src/components/NewPlaylistButton/index.tsx

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import { PlusIcon } from "@radix-ui/react-icons";
2-
import { Button, Dialog, Flex, TextField } from "@radix-ui/themes";
2+
import {
3+
Button,
4+
Dialog,
5+
Flex,
6+
Select,
7+
Text,
8+
TextField,
9+
} from "@radix-ui/themes";
310
import { type FC, useMemo, useState } from "react";
411
import { Trans, useTranslation } from "react-i18next";
512
import { db } from "../../dexie.ts";
@@ -33,12 +40,26 @@ export const NewPlaylistButton: FC = () => {
3340
<Dialog.Title>
3441
<Trans i18nKey="newPlaylist.dialog.title">新建歌单</Trans>
3542
</Dialog.Title>
36-
<TextField.Root
37-
placeholder={t("newPlaylist.dialog.namePlaceholder", "歌单名称")}
38-
value={name}
39-
onChange={(e) => setName(e.currentTarget.value)}
40-
autoFocus
41-
/>
43+
<Flex gap="3" direction="column">
44+
<Text>
45+
<Trans i18nKey="newPlaylist.dialog.name">歌单名称</Trans>
46+
</Text>
47+
<TextField.Root
48+
placeholder={t("newPlaylist.dialog.namePlaceholder", "歌单名称")}
49+
value={name}
50+
onChange={(e) => setName(e.currentTarget.value)}
51+
autoFocus
52+
/>
53+
<Select.Root>
54+
<Select.Trigger placeholder="歌单管理源" />
55+
<Select.Content>
56+
<Select.Item value="amll-player:local">本地歌曲源</Select.Item>
57+
<Select.Item value="amll-player:android-music">
58+
安卓内容提供者 - 音频媒体源
59+
</Select.Item>
60+
</Select.Content>
61+
</Select.Root>
62+
</Flex>
4263
<Flex gap="3" mt="4" justify="end">
4364
<Dialog.Close>
4465
<Button type="button" variant="soft" color="gray">

0 commit comments

Comments
 (0)