|
| 1 | +import type { LyricLine } from "@applemusic-like-lyrics/core"; |
| 2 | +import "@applemusic-like-lyrics/core/style.css"; |
| 3 | +import { LyricPlayer } from "@applemusic-like-lyrics/react"; |
| 4 | +import { useLayoutEffect, useRef, useState } from "react"; |
| 5 | + |
| 6 | +const buildLyricLines = ( |
| 7 | + lyric: string, |
| 8 | + startTime = 1000, |
| 9 | + otherParams: Partial<LyricLine> = {}, |
| 10 | +): LyricLine => { |
| 11 | + let curTime = startTime; |
| 12 | + const words = []; |
| 13 | + for (const word of lyric.split("|")) { |
| 14 | + const [text, duration] = word.split(","); |
| 15 | + const endTime = curTime + Number.parseInt(duration); |
| 16 | + words.push({ |
| 17 | + word: text, |
| 18 | + startTime: curTime, |
| 19 | + endTime, |
| 20 | + obscene: false, |
| 21 | + }); |
| 22 | + curTime = endTime; |
| 23 | + } |
| 24 | + return { |
| 25 | + startTime, |
| 26 | + endTime: curTime + 3000, |
| 27 | + translatedLyric: "", |
| 28 | + romanLyric: "", |
| 29 | + isBG: false, |
| 30 | + isDuet: false, |
| 31 | + words, |
| 32 | + ...otherParams, |
| 33 | + }; |
| 34 | +}; |
| 35 | + |
| 36 | +const DEMO_LYRICS: LyricLine[][] = [ |
| 37 | + [ |
| 38 | + buildLyricLines( |
| 39 | + "Apple ,750|Music ,500|Like ,500|Ly,400|ri,500|cs ,250", |
| 40 | + 1000, |
| 41 | + { |
| 42 | + translatedLyric: "类苹果歌词", |
| 43 | + }, |
| 44 | + ), |
| 45 | + // A lyric component library for the web |
| 46 | + buildLyricLines( |
| 47 | + "A ,400|ly,500|ric ,250|com,500|po,500|nent ,500|li,500|bra,500|ry ,500|for ,500|the ,500|web ,500", |
| 48 | + 7000, |
| 49 | + { |
| 50 | + translatedLyric: "为 Web 而生的歌词组件库", |
| 51 | + }, |
| 52 | + ), |
| 53 | + // Brought to you with |
| 54 | + buildLyricLines("Brought ,500|to ,250|you ,800|with ,600", 16000, { |
| 55 | + translatedLyric: "为你带来", |
| 56 | + }), |
| 57 | + // Background Lyric Line |
| 58 | + buildLyricLines("Background ,750|Lyric ,300|Line ,500", 16500, { |
| 59 | + translatedLyric: "背景歌词行", |
| 60 | + isBG: true, |
| 61 | + }), |
| 62 | + // And Duet Lyric Line |
| 63 | + buildLyricLines("And ,300|Duet ,500|Lyric ,500|Line ,750", 21150, { |
| 64 | + translatedLyric: "还有对唱歌词行", |
| 65 | + isDuet: true, |
| 66 | + }), |
| 67 | + ], |
| 68 | +]; |
| 69 | + |
| 70 | +export const AMLLPreview = () => { |
| 71 | + const [lyricLines, setLyricLines] = useState<LyricLine[]>([]); |
| 72 | + const [currentTime, setCurrentTime] = useState(0); |
| 73 | + const wRef = useRef<HTMLDivElement>(null); |
| 74 | + |
| 75 | + useLayoutEffect(() => { |
| 76 | + let selectedDemo = DEMO_LYRICS.length - 1; |
| 77 | + let endTime = 0; |
| 78 | + let startTime = 0; |
| 79 | + let canceled = false; |
| 80 | + |
| 81 | + const onFrame = (time: number) => { |
| 82 | + if (canceled) return; |
| 83 | + if (time - startTime > endTime) { |
| 84 | + const w = wRef.current; |
| 85 | + if (!w) { |
| 86 | + if (canceled) return; |
| 87 | + return; |
| 88 | + } |
| 89 | + if (canceled) return; |
| 90 | + |
| 91 | + w.animate( |
| 92 | + { |
| 93 | + opacity: 0, |
| 94 | + filter: "blur(10px)", |
| 95 | + }, |
| 96 | + { |
| 97 | + duration: 500, |
| 98 | + easing: "ease-in-out", |
| 99 | + fill: "forwards", |
| 100 | + }, |
| 101 | + ).onfinish = () => { |
| 102 | + if (canceled) return; |
| 103 | + |
| 104 | + selectedDemo = (selectedDemo + 1) % DEMO_LYRICS.length; |
| 105 | + setLyricLines(JSON.parse(JSON.stringify(DEMO_LYRICS[selectedDemo]))); |
| 106 | + endTime = DEMO_LYRICS[selectedDemo].reduce( |
| 107 | + (acc, v) => Math.max(acc, v.endTime), |
| 108 | + 0, |
| 109 | + ); |
| 110 | + startTime = time; |
| 111 | + |
| 112 | + setTimeout(() => { |
| 113 | + if (canceled) return; |
| 114 | + w.animate( |
| 115 | + { |
| 116 | + opacity: 1, |
| 117 | + filter: "blur(0px)", |
| 118 | + }, |
| 119 | + { |
| 120 | + duration: 500, |
| 121 | + easing: "ease-in-out", |
| 122 | + fill: "forwards", |
| 123 | + }, |
| 124 | + ).onfinish = () => { |
| 125 | + if (canceled) return; |
| 126 | + requestAnimationFrame(onFrame); |
| 127 | + }; |
| 128 | + }, 1000); |
| 129 | + }; |
| 130 | + } else { |
| 131 | + setCurrentTime((time - startTime) | 0); |
| 132 | + requestAnimationFrame(onFrame); |
| 133 | + } |
| 134 | + }; |
| 135 | + |
| 136 | + requestAnimationFrame(onFrame); |
| 137 | + return () => { |
| 138 | + canceled = true; |
| 139 | + }; |
| 140 | + }, []); |
| 141 | + |
| 142 | + return ( |
| 143 | + <div |
| 144 | + style={{ |
| 145 | + height: "100%", |
| 146 | + maskImage: |
| 147 | + "linear-gradient(to bottom, transparent 0%, white 5%, white 95%, transparent 100%)", |
| 148 | + transition: "opacity 0.5s, filter 0.5s", |
| 149 | + }} |
| 150 | + ref={wRef} |
| 151 | + > |
| 152 | + <LyricPlayer |
| 153 | + currentTime={currentTime} |
| 154 | + lyricLines={lyricLines} |
| 155 | + alignAnchor="top" |
| 156 | + alignPosition={0.05} |
| 157 | + style={{ |
| 158 | + height: "100%", |
| 159 | + }} |
| 160 | + /> |
| 161 | + </div> |
| 162 | + ); |
| 163 | +}; |
0 commit comments