157277364S猫头猫/** 257277364S猫头猫 * 管理当前歌曲的歌词 357277364S猫头猫 */ 457277364S猫头猫 557277364S猫头猫import {isSameMediaItem} from '@/utils/mediaItem'; 657277364S猫头猫import PluginManager from './pluginManager'; 757277364S猫头猫import LyricParser from '@/utils/lrcParser'; 857277364S猫头猫import {GlobalState} from '@/utils/stateMapper'; 957277364S猫头猫import {EDeviceEvents} from '@/constants/commonConst'; 1057277364S猫头猫import {DeviceEventEmitter} from 'react-native'; 110be848bcS猫头猫import Config from './config'; 120be848bcS猫头猫import LyricUtil from '@/native/lyricUtil'; 13*5500cea7S猫头猫import TrackPlayer from './trackPlayer'; 1457277364S猫头猫 1557277364S猫头猫const lyricStateStore = new GlobalState<{ 1657277364S猫头猫 loading: boolean; 1757277364S猫头猫 lyricParser?: LyricParser; 1857277364S猫头猫 lyrics: ILyric.IParsedLrc; 1957277364S猫头猫 meta?: Record<string, string>; 2057277364S猫头猫}>({ 2157277364S猫头猫 loading: true, 2257277364S猫头猫 lyrics: [], 2357277364S猫头猫}); 2457277364S猫头猫 2557277364S猫头猫const currentLyricStore = new GlobalState<ILyric.IParsedLrcItem | null>(null); 2657277364S猫头猫 2757277364S猫头猫// 重新获取歌词 2857277364S猫头猫async function refreshLyric(fromStart?: boolean) { 29*5500cea7S猫头猫 const musicItem = TrackPlayer.getCurrentMusic(); 3057277364S猫头猫 try { 31*5500cea7S猫头猫 if (!musicItem) { 32*5500cea7S猫头猫 lyricStateStore.setValue({ 33*5500cea7S猫头猫 loading: false, 34*5500cea7S猫头猫 lyrics: [], 35*5500cea7S猫头猫 }); 36*5500cea7S猫头猫 37*5500cea7S猫头猫 currentLyricStore.setValue({ 38*5500cea7S猫头猫 lrc: 'MusicFree', 39*5500cea7S猫头猫 time: 0, 40*5500cea7S猫头猫 }); 41*5500cea7S猫头猫 42*5500cea7S猫头猫 return; 43*5500cea7S猫头猫 } 44*5500cea7S猫头猫 4557277364S猫头猫 lyricStateStore.setValue({ 4657277364S猫头猫 loading: true, 4757277364S猫头猫 lyrics: [], 4857277364S猫头猫 }); 4957277364S猫头猫 currentLyricStore.setValue(null); 5057277364S猫头猫 5157277364S猫头猫 const lrc = await PluginManager.getByMedia( 5257277364S猫头猫 musicItem, 5357277364S猫头猫 )?.methods?.getLyricText(musicItem); 54*5500cea7S猫头猫 const realtimeMusicItem = TrackPlayer.getCurrentMusic(); 5557277364S猫头猫 if (isSameMediaItem(musicItem, realtimeMusicItem)) { 5657277364S猫头猫 if (lrc) { 5757277364S猫头猫 const parser = new LyricParser(lrc, musicItem); 5857277364S猫头猫 lyricStateStore.setValue({ 5957277364S猫头猫 loading: false, 6057277364S猫头猫 lyricParser: parser, 6157277364S猫头猫 lyrics: parser.getLyric(), 6257277364S猫头猫 meta: parser.getMeta(), 6357277364S猫头猫 }); 6457277364S猫头猫 // 更新当前状态的歌词 6557277364S猫头猫 const currentLyric = fromStart 6657277364S猫头猫 ? parser.getLyric()[0] 67*5500cea7S猫头猫 : parser.getPosition( 68*5500cea7S猫头猫 (await TrackPlayer.getProgress()).position, 69*5500cea7S猫头猫 ).lrc; 7057277364S猫头猫 currentLyricStore.setValue(currentLyric || null); 7157277364S猫头猫 } else { 7257277364S猫头猫 // 没有歌词 7357277364S猫头猫 lyricStateStore.setValue({ 7457277364S猫头猫 loading: false, 7557277364S猫头猫 lyrics: [], 7657277364S猫头猫 }); 7757277364S猫头猫 } 7857277364S猫头猫 } 7957277364S猫头猫 } catch { 80*5500cea7S猫头猫 const realtimeMusicItem = TrackPlayer.getCurrentMusic(); 8157277364S猫头猫 if (isSameMediaItem(musicItem, realtimeMusicItem)) { 8257277364S猫头猫 // 异常情况 8357277364S猫头猫 lyricStateStore.setValue({ 8457277364S猫头猫 loading: false, 8557277364S猫头猫 lyrics: [], 8657277364S猫头猫 }); 8757277364S猫头猫 } 8857277364S猫头猫 } 8957277364S猫头猫} 9057277364S猫头猫 9157277364S猫头猫// 获取歌词 9257277364S猫头猫async function setup() { 9357277364S猫头猫 DeviceEventEmitter.addListener(EDeviceEvents.REFRESH_LYRIC, refreshLyric); 9457277364S猫头猫 950be848bcS猫头猫 if (Config.get('setting.lyric.showStatusBarLyric')) { 960be848bcS猫头猫 LyricUtil.showStatusBarLyric( 970be848bcS猫头猫 'MusicFree', 980be848bcS猫头猫 Config.get('setting.lyric') ?? {}, 990be848bcS猫头猫 ); 1000be848bcS猫头猫 } 1010be848bcS猫头猫 10257277364S猫头猫 refreshLyric(); 10357277364S猫头猫} 10457277364S猫头猫 10557277364S猫头猫const LyricManager = { 10657277364S猫头猫 setup, 10757277364S猫头猫 useLyricState: lyricStateStore.useValue, 10857277364S猫头猫 getLyricState: lyricStateStore.getValue, 10957277364S猫头猫 useCurrentLyric: currentLyricStore.useValue, 11057277364S猫头猫 getCurrentLyric: currentLyricStore.getValue, 11157277364S猫头猫 setCurrentLyric: currentLyricStore.setValue, 11257277364S猫头猫}; 11357277364S猫头猫 11457277364S猫头猫export default LyricManager; 115