diff --git a/src/hooks/MusicHook.ts b/src/hooks/MusicHook.ts new file mode 100644 index 0000000..07d15c3 --- /dev/null +++ b/src/hooks/MusicHook.ts @@ -0,0 +1,104 @@ +import { getMusicLrc } from '@/api/music' +import { ILyric } from '@/type/lyric' +import { ref } from 'vue' + +interface ILrcData { + text: string + trText: string +} + +const lrcData = ref() +const newLrcIndex = ref(0) +const lrcArray = ref>([]) +const lrcTimeArray = ref>([]) + +const parseTime = (timeString: string) => { + const [minutes, seconds] = timeString.split(':') + return parseInt(minutes) * 60 + parseFloat(seconds) +} + +const TIME_REGEX = /(\d{2}:\d{2}(\.\d*)?)/g +const LRC_REGEX = /(\[(\d{2}):(\d{2})(\.(\d*))?\])/g + +function parseLyricLine(lyricLine: string) { + // [00:00.00] 作词 : 长友美知惠/ + const timeText = lyricLine.match(TIME_REGEX)?.[0] || '' + const time = parseTime(timeText) + const text = lyricLine.replace(LRC_REGEX, '').trim() + return { time, text } +} + +interface ILyricText { + text: string + trText: string +} + +function parseLyrics(lyricsString: string) { + const lines = lyricsString.split('\n') + const lyrics: Array = [] + const times: number[] = [] + lines.forEach((line) => { + const { time, text } = parseLyricLine(line) + times.push(time) + lyrics.push({ text, trText: '' }) + }) + return { lyrics, times } +} + +const loadLrc = async (playMusicId: number): Promise => { + try { + const { data } = await getMusicLrc(playMusicId) + const { lyrics, times } = parseLyrics(data.lrc.lyric) + lrcTimeArray.value = times + lrcArray.value = lyrics + } catch (err) { + console.error('err', err) + } +} + +// 歌词矫正时间Correction time +const correctionTime = ref(0) + +// 增加矫正时间 +const addCorrectionTime = (time: number) => { + correctionTime.value += time +} + +// 减少矫正时间 +const reduceCorrectionTime = (time: number) => { + correctionTime.value -= time +} + +const isCurrentLrc = (index: any, time: number) => { + const currentTime = Number(lrcTimeArray.value[index]) + const nextTime = Number(lrcTimeArray.value[index + 1]) + const nowTime = time + correctionTime.value + const isTrue = nowTime > currentTime && nowTime < nextTime + if (isTrue) { + newLrcIndex.value = index + } + return isTrue +} + +const nowTime = ref(0) +const allTime = ref(0) + +// 设置当前播放时间 +const setAudioTime = (index: any, audio: HTMLAudioElement) => { + audio.currentTime = lrcTimeArray.value[index] as number + audio.play() +} + +export { + lrcData, + lrcArray, + lrcTimeArray, + newLrcIndex, + loadLrc, + isCurrentLrc, + addCorrectionTime, + reduceCorrectionTime, + setAudioTime, + nowTime, + allTime, +} diff --git a/src/index.css b/src/index.css index cc30601..e4292cd 100644 --- a/src/index.css +++ b/src/index.css @@ -5,10 +5,7 @@ @tailwind components; @tailwind utilities; -img{ - @apply bg-gray-900; -} - .n-image img { + @apply bg-gray-900; width: 100%; } \ No newline at end of file diff --git a/src/layout/components/MusicFull.vue b/src/layout/components/MusicFull.vue index 46bb27f..4cce82f 100644 --- a/src/layout/components/MusicFull.vue +++ b/src/layout/components/MusicFull.vue @@ -1,188 +1,152 @@ - - - \ No newline at end of file diff --git a/src/views/home/index.vue b/src/views/home/index.vue index 8384dbe..e04f256 100644 --- a/src/views/home/index.vue +++ b/src/views/home/index.vue @@ -15,35 +15,10 @@