5656
5757<script setup lang="ts">
5858import { LyricLineMouseEvent , type LyricLine } from " @applemusic-like-lyrics/core" ;
59+ import { type LyricPlayerRef } from " @/components/AMLL/LyricPlayer.vue" ;
5960import { useMusicStore , useSettingStore , useStatusStore } from " @/stores" ;
6061import { getLyricLanguage } from " @/utils/format" ;
6162import { usePlayerController } from " @/core/player/PlayerController" ;
@@ -75,7 +76,7 @@ const statusStore = useStatusStore();
7576const settingStore = useSettingStore ();
7677const player = usePlayerController ();
7778
78- const lyricPlayerRef = ref <any | null >(null );
79+ const lyricPlayerRef = ref <LyricPlayerRef | null >(null );
7980
8081// 当前歌词
8182const amLyricsData = computed (() => {
@@ -94,7 +95,7 @@ const amLyricsData = computed(() => {
9495 // 处理显隐
9596 if (! showTran ) line .translatedLyric = " " ;
9697 if (! showRoma ) line .romanLyric = " " ;
97- if (! showWordsRoma ) line .words ?.forEach ((word ) => ( word .romanWord = " " ) );
98+ if (! showWordsRoma ) line .words ?.forEach ((word ) => delete word .romanWord );
9899 // 调换翻译与音译位置
99100 if (swapTranRoma ) {
100101 const temp = line .translatedLyric ;
@@ -115,29 +116,44 @@ const hasDuet = computed(() => amLyricsData.value?.some((line) => line.isDuet) ?
115116// 进度跳转
116117const jumpSeek = (line : LyricLineMouseEvent ) => {
117118 const lineContent = line .line .getLine ();
118- if (! lineContent ?.startTime ) return ;
119- const time = lineContent .startTime ;
119+ const lyricTargetTime = lineContent ?.startTime ;
120+ if (
121+ typeof lyricTargetTime !== " number" ||
122+ ! Number .isFinite (lyricTargetTime ) ||
123+ lyricTargetTime < 0
124+ ) {
125+ return ;
126+ }
127+ // 让 LyricPlayer 跳转到目标时间,第二个参数 isSeek = true 会重置滚动状态
128+ lyricPlayerRef .value ?.setCurrentTime (lyricTargetTime , true );
129+ // 获取偏移时间,计算歌曲真实的目标时间,并跳转
120130 const offsetMs = statusStore .getSongOffset (musicStore .playSong ?.id );
121- player .setSeek (time - offsetMs );
131+ const musicTargetTime = lyricTargetTime - offsetMs ;
132+ player .setSeek (musicTargetTime );
122133 player .play ();
123134};
124135
125136// 处理歌词语言
126137const processLyricLanguage = (player = lyricPlayerRef .value ) => {
127- const lyricLineObjects = player ?.lyricPlayer ?. currentLyricLineObjects ;
138+ const lyricLineObjects = player ?.currentLyricLineObjects ;
128139 if (! Array .isArray (lyricLineObjects ) || lyricLineObjects .length === 0 ) {
129140 return ;
130141 }
131142 // 遍历歌词行
132143 for (let e of lyricLineObjects ) {
133144 // 获取歌词行内容 (合并逐字歌词为一句)
134- const content = e .lyricLine .words .map ((word : any ) => word .word ).join (" " );
145+ const content = e .lyricLine .words .map ((word ) => word .word ).join (" " );
135146 // 跳过空行
136147 if (! content ) continue ;
137148 // 获取歌词语言
138149 const lang = getLyricLanguage (content );
139150 // 为主歌词设置 lang 属性 (firstChild 获取主歌词 不为翻译和音译设置属性)
140- e .element .firstChild .setAttribute (" lang" , lang );
151+ const lyricMainLineElement = e .element ?.firstChild ;
152+ if (lyricMainLineElement instanceof HTMLElement ) {
153+ lyricMainLineElement .setAttribute (" lang" , lang );
154+ } else {
155+ console .warn (" 无法获取歌词行元素的主歌词部分,无法设置 lang 属性" , e .element );
156+ }
141157 }
142158};
143159
0 commit comments