Skip to content

Commit bdfbb4d

Browse files
committed
🐞 fix: 修复网页端获取 ttml
1 parent 6937a93 commit bdfbb4d

8 files changed

Lines changed: 38 additions & 33 deletions

File tree

components.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ declare module 'vue' {
106106
NSlider: typeof import('naive-ui')['NSlider']
107107
NSpin: typeof import('naive-ui')['NSpin']
108108
NSwitch: typeof import('naive-ui')['NSwitch']
109+
NTab: typeof import('naive-ui')['NTab']
109110
NTabPane: typeof import('naive-ui')['NTabPane']
110111
NTabs: typeof import('naive-ui')['NTabs']
111112
NTag: typeof import('naive-ui')['NTag']

electron/server/netease/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export const initNcmAPI = async (fastify: FastifyInstance) => {
7878
return reply.send(data);
7979
} catch (error) {
8080
serverLog.error("❌ TTML Lyric Fetch Error:", error);
81-
return reply.status(500).send(null);
81+
return reply.send(null);
8282
}
8383
},
8484
);

src/api/song.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { isElectron } from "@/utils/helper";
12
import { songLevelData } from "@/utils/meta";
23
import request from "@/utils/request";
34

@@ -70,8 +71,22 @@ export const songLyric = (id: number) => {
7071
* @param id 音乐 id
7172
* @returns TTML 格式歌词
7273
*/
73-
export const songLyricTTML = (id: number) => {
74-
return request({ url: "/lyric/ttml", params: { id } });
74+
export const songLyricTTML = async (id: number) => {
75+
if (isElectron) {
76+
return request({ url: "/lyric/ttml", params: { id } });
77+
} else {
78+
const url = `https://amll-ttml-db.stevexmh.net/ncm/${id}`;
79+
try {
80+
const response = await fetch(url);
81+
if (response === null || response.status !== 200) {
82+
return null;
83+
}
84+
const data = await response.text();
85+
return data;
86+
} catch {
87+
return null;
88+
}
89+
}
7590
};
7691

7792
/**

src/components/Player/MainAMLyric.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template>
2-
<Transition>
2+
<Transition name="fade" mode="out-in">
33
<div
4-
:key="amLyricsData?.[0]?.startTime"
4+
:key="amLyricsData?.[0]?.words?.length"
55
:class="['lyric-am', { pure: statusStore.pureLyricMode }]"
66
>
77
<LyricPlayer

src/stores/status.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export const useStatusStore = defineStore("status", {
100100
searchInputValue: "",
101101
showPlayBar: true,
102102
playStatus: false,
103-
playLoading: false,
103+
playLoading: true,
104104
playUblock: false,
105105
playListShow: false,
106106
showFullPlayer: false,

src/utils/init.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const init = async () => {
2525

2626
// 加载数据
2727
await dataStore.loadData();
28+
2829
// 初始化播放器
2930
player.initPlayer(
3031
settingStore.autoPlay,

src/utils/player.ts

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -70,22 +70,10 @@ class Player {
7070
private isStale(sessionId: number): boolean {
7171
return sessionId !== this.playSessionId;
7272
}
73-
/**
74-
* 保护执行:会话过期则早退
75-
*/
76-
private guard<T>(sessionId: number, fn: () => T): T | undefined {
77-
if (this.isStale(sessionId)) return;
78-
return fn();
79-
}
8073
/**
8174
* 重置底层播放器与定时器(幂等)
8275
*/
8376
private resetPlayerCore() {
84-
try {
85-
this.player?.off();
86-
} catch {
87-
/* empty */
88-
}
8977
try {
9078
Howler.stop();
9179
Howler.unload();
@@ -248,23 +236,20 @@ class Player {
248236
// 自动播放
249237
if (autoPlay) await this.play();
250238
// 获取歌曲附加信息 - 非电台和本地
251-
if (type !== "radio" && !path) {
252-
runIdle(() => getLyricData(id));
253-
} else resetSongLyric();
239+
if (type !== "radio" && !path) getLyricData(id);
240+
else resetSongLyric();
254241
// 定时获取状态
255242
if (!this.playerInterval) this.handlePlayStatus();
256243
// 新增播放历史
257244
if (type !== "radio") dataStore.setHistory(musicStore.playSong);
258245
// 获取歌曲封面主色
259246
if (!path) runIdle(() => getCoverColor(musicStore.songCover));
260247
// 更新 MediaSession
261-
if (!path) runIdle(() => this.updateMediaSession());
248+
if (!path) this.updateMediaSession();
262249
// 开发模式
263250
if (isDev) window.player = this.player;
264251
// 预载下一首播放地址
265-
runIdle(() => {
266-
void this.prefetchNextSongUrl();
267-
});
252+
this.prefetchNextSongUrl();
268253
}
269254
/**
270255
* 播放器事件
@@ -554,7 +539,7 @@ class Player {
554539
const { lyric, format } = await window.electron.ipcRenderer.invoke("get-music-lyric", path);
555540
parseLocalLyric(lyric, format);
556541
// 更新媒体会话
557-
runIdle(() => this.updateMediaSession());
542+
this.updateMediaSession();
558543
} catch (error) {
559544
window.$message.error("获取本地歌曲元信息失败");
560545
console.error("Failed to parse local music info:", error);
@@ -595,7 +580,10 @@ class Player {
595580
try {
596581
// 获取播放数据
597582
const playSongData = getPlaySongData();
598-
if (!playSongData) return;
583+
if (!playSongData) {
584+
statusStore.playLoading = false;
585+
return;
586+
}
599587
const { id, dj, path, type } = playSongData;
600588
// 更改当前播放歌曲
601589
musicStore.playSong = playSongData;
@@ -695,7 +683,7 @@ class Player {
695683
const settingStore = useSettingStore();
696684
// 检查播放器状态
697685
if (!this.player || this.player.state() === "unloaded") {
698-
console.warn("⚠️ Player not ready for play");
686+
window.$message.warning("播放器未加载完成,请稍后重试");
699687
return;
700688
}
701689
// 已在播放
@@ -721,7 +709,6 @@ class Player {
721709
async pause(changeStatus: boolean = true) {
722710
const statusStore = useStatusStore();
723711
const settingStore = useSettingStore();
724-
const localSession = this.playSessionId;
725712

726713
// 播放器未加载完成或不存在
727714
if (!this.player || this.player.state() !== "loaded") {
@@ -734,7 +721,7 @@ class Player {
734721
await new Promise<void>((resolve) => {
735722
this.player.fade(statusStore.playVolume, 0, settingStore.getFadeTime);
736723
this.player.once("fade", () => {
737-
this.guard(localSession, () => this.player.pause());
724+
this.player.pause();
738725
resolve();
739726
});
740727
});

src/views/Home/HomeOnline.vue

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ const recData = ref<RecDataType>({
136136
// 获取全部推荐
137137
const getAllRecData = async () => {
138138
try {
139-
// 延时 50ms
140-
await sleep(50);
139+
// 延时
140+
await sleep(300);
141141
142142
// 歌单
143143
try {
@@ -199,9 +199,10 @@ const getAllRecData = async () => {
199199
}
200200
};
201201
202+
onActivated(getAllRecData);
203+
202204
onMounted(() => {
203205
getAllRecData();
204-
onActivated(getAllRecData);
205206
});
206207
</script>
207208

0 commit comments

Comments
 (0)