xref: /MusicFree/src/core/trackPlayer/index.ts (revision 43eb30bf16ea88ab8e3cdff98faf307c8c8f93d3)
15500cea7S猫头猫import produce from 'immer';
25500cea7S猫头猫import ReactNativeTrackPlayer, {
35500cea7S猫头猫    Event,
45500cea7S猫头猫    State,
55500cea7S猫头猫    Track,
65500cea7S猫头猫    TrackMetadataBase,
75500cea7S猫头猫    usePlaybackState,
85500cea7S猫头猫    useProgress,
95500cea7S猫头猫} from 'react-native-track-player';
105500cea7S猫头猫import shuffle from 'lodash.shuffle';
115500cea7S猫头猫import Config from '../config';
125500cea7S猫头猫import {
135500cea7S猫头猫    EDeviceEvents,
145500cea7S猫头猫    internalFakeSoundKey,
155500cea7S猫头猫    sortIndexSymbol,
165500cea7S猫头猫    timeStampSymbol,
175500cea7S猫头猫} from '@/constants/commonConst';
185500cea7S猫头猫import {GlobalState} from '@/utils/stateMapper';
195500cea7S猫头猫import delay from '@/utils/delay';
205500cea7S猫头猫import {
215500cea7S猫头猫    isSameMediaItem,
225500cea7S猫头猫    mergeProps,
235500cea7S猫头猫    sortByTimestampAndIndex,
245500cea7S猫头猫} from '@/utils/mediaItem';
255500cea7S猫头猫import Network from '../network';
265500cea7S猫头猫import LocalMusicSheet from '../localMusicSheet';
275500cea7S猫头猫import {SoundAsset} from '@/constants/assetsConst';
285500cea7S猫头猫import {getQualityOrder} from '@/utils/qualities';
295500cea7S猫头猫import musicHistory from '../musicHistory';
305500cea7S猫头猫import getUrlExt from '@/utils/getUrlExt';
315500cea7S猫头猫import {DeviceEventEmitter} from 'react-native';
325500cea7S猫头猫import LyricManager from '../lyricManager';
335500cea7S猫头猫import {MusicRepeatMode} from './common';
345500cea7S猫头猫import {
355500cea7S猫头猫    getMusicIndex,
365500cea7S猫头猫    getPlayList,
375500cea7S猫头猫    getPlayListMusicAt,
385500cea7S猫头猫    isInPlayList,
395500cea7S猫头猫    isPlayListEmpty,
405500cea7S猫头猫    setPlayList,
415500cea7S猫头猫    usePlayList,
425500cea7S猫头猫} from './internal/playList';
435500cea7S猫头猫import {createMediaIndexMap} from '@/utils/mediaIndexMap';
445500cea7S猫头猫import PluginManager from '../pluginManager';
455500cea7S猫头猫import {musicIsPaused} from '@/utils/trackUtils';
46f511aee9S猫头猫import Toast from '@/utils/toast';
4762e73a5eS猫头猫import {trace} from '@/utils/log';
485500cea7S猫头猫
495500cea7S猫头猫/** 当前播放 */
505500cea7S猫头猫const currentMusicStore = new GlobalState<IMusic.IMusicItem | null>(null);
515500cea7S猫头猫
525500cea7S猫头猫/** 播放模式 */
535500cea7S猫头猫const repeatModeStore = new GlobalState<MusicRepeatMode>(MusicRepeatMode.QUEUE);
545500cea7S猫头猫
555500cea7S猫头猫/** 音质 */
565500cea7S猫头猫const qualityStore = new GlobalState<IMusic.IQualityKey>('standard');
575500cea7S猫头猫
585500cea7S猫头猫let currentIndex = -1;
595500cea7S猫头猫
605500cea7S猫头猫// TODO: 下个版本最大限制调大一些
615500cea7S猫头猫const maxMusicQueueLength = 1500; // 当前播放最大限制
625500cea7S猫头猫const halfMaxMusicQueueLength = Math.floor(maxMusicQueueLength / 2);
635500cea7S猫头猫const shrinkPlayListToSize = (
645500cea7S猫头猫    queue: IMusic.IMusicItem[],
655500cea7S猫头猫    targetIndex = currentIndex,
665500cea7S猫头猫) => {
675500cea7S猫头猫    // 播放列表上限,太多无法缓存状态
685500cea7S猫头猫    if (queue.length > maxMusicQueueLength) {
695500cea7S猫头猫        if (targetIndex < halfMaxMusicQueueLength) {
705500cea7S猫头猫            queue = queue.slice(0, maxMusicQueueLength);
715500cea7S猫头猫        } else {
725500cea7S猫头猫            const right = Math.min(
735500cea7S猫头猫                queue.length,
745500cea7S猫头猫                targetIndex + halfMaxMusicQueueLength,
755500cea7S猫头猫            );
765500cea7S猫头猫            const left = Math.max(0, right - maxMusicQueueLength);
775500cea7S猫头猫            queue = queue.slice(left, right);
785500cea7S猫头猫        }
795500cea7S猫头猫    }
805500cea7S猫头猫    return queue;
815500cea7S猫头猫};
825500cea7S猫头猫
837aed04d4S猫头猫let hasSetupListener = false;
847aed04d4S猫头猫
855500cea7S猫头猫async function setupTrackPlayer() {
865500cea7S猫头猫    const config = Config.get('status.music') ?? {};
875500cea7S猫头猫    const {rate, repeatMode, musicQueue, progress, track} = config;
885500cea7S猫头猫
895500cea7S猫头猫    // 状态恢复
905500cea7S猫头猫    if (rate) {
915500cea7S猫头猫        await ReactNativeTrackPlayer.setRate(+rate / 100);
925500cea7S猫头猫    }
935500cea7S猫头猫
945500cea7S猫头猫    if (musicQueue && Array.isArray(musicQueue)) {
955500cea7S猫头猫        addAll(musicQueue, undefined, repeatMode === MusicRepeatMode.SHUFFLE);
965500cea7S猫头猫    }
975500cea7S猫头猫
985500cea7S猫头猫    const currentQuality =
995500cea7S猫头猫        Config.get('setting.basic.defaultPlayQuality') ?? 'standard';
1005500cea7S猫头猫
1015500cea7S猫头猫    if (track && isInPlayList(track)) {
1025500cea7S猫头猫        const newSource = await PluginManager.getByMedia(
1035500cea7S猫头猫            track,
1045500cea7S猫头猫        )?.methods.getMediaSource(track, currentQuality, 0);
1055500cea7S猫头猫        // 重新初始化 获取最新的链接
1065500cea7S猫头猫        track.url = newSource?.url || track.url;
1075500cea7S猫头猫        track.headers = newSource?.headers || track.headers;
1085500cea7S猫头猫
1095500cea7S猫头猫        await setTrackSource(track as Track, false);
1105500cea7S猫头猫        setCurrentMusic(track);
1115500cea7S猫头猫
1125500cea7S猫头猫        if (config?.progress) {
1135500cea7S猫头猫            await ReactNativeTrackPlayer.seekTo(progress!);
1145500cea7S猫头猫        }
1155500cea7S猫头猫    }
1165500cea7S猫头猫
1177aed04d4S猫头猫    if (!hasSetupListener) {
1185500cea7S猫头猫        ReactNativeTrackPlayer.addEventListener(
1195500cea7S猫头猫            Event.PlaybackActiveTrackChanged,
1205500cea7S猫头猫            async evt => {
1215500cea7S猫头猫                if (
1225500cea7S猫头猫                    evt.index === 1 &&
1235500cea7S猫头猫                    evt.lastIndex === 0 &&
1245500cea7S猫头猫                    evt.track?.$ === internalFakeSoundKey
1255500cea7S猫头猫                ) {
12662e73a5eS猫头猫                    trace('队列末尾,播放下一首');
1275500cea7S猫头猫                    if (repeatModeStore.getValue() === MusicRepeatMode.SINGLE) {
1285500cea7S猫头猫                        await play(null, true);
1295500cea7S猫头猫                    } else {
1305500cea7S猫头猫                        // 当前生效的歌曲是下一曲的标记
1316f57784cS猫头猫                        await skipToNext();
1325500cea7S猫头猫                    }
1335500cea7S猫头猫                }
1345500cea7S猫头猫            },
1355500cea7S猫头猫        );
1365500cea7S猫头猫
1377aed04d4S猫头猫        ReactNativeTrackPlayer.addEventListener(
1387aed04d4S猫头猫            Event.PlaybackError,
13962e73a5eS猫头猫            async e => {
14062e73a5eS猫头猫                // WARNING: 不稳定,报错的时候有可能track已经变到下一首歌去了
1417aed04d4S猫头猫                if (
14262e73a5eS猫头猫                    (await ReactNativeTrackPlayer.getActiveTrackIndex()) ===
14362e73a5eS猫头猫                        0 &&
14462e73a5eS猫头猫                    e.message &&
14562e73a5eS猫头猫                    e.message !== 'android-io-file-not-found'
1467aed04d4S猫头猫                ) {
14762e73a5eS猫头猫                    trace('播放出错', {
14862e73a5eS猫头猫                        message: e.message,
14962e73a5eS猫头猫                        code: e.code,
15062e73a5eS猫头猫                    });
1515500cea7S猫头猫                    failToPlay();
1525500cea7S猫头猫                }
1537aed04d4S猫头猫            },
1547aed04d4S猫头猫        );
1557aed04d4S猫头猫
1567aed04d4S猫头猫        hasSetupListener = true;
1577aed04d4S猫头猫    }
1585500cea7S猫头猫}
1595500cea7S猫头猫
1605500cea7S猫头猫/**
1615500cea7S猫头猫 * 获取自动播放的下一个track
1625500cea7S猫头猫 */
1635500cea7S猫头猫const getFakeNextTrack = () => {
1645500cea7S猫头猫    let track: Track | undefined;
1655500cea7S猫头猫    const repeatMode = repeatModeStore.getValue();
1665500cea7S猫头猫    if (repeatMode === MusicRepeatMode.SINGLE) {
1675500cea7S猫头猫        // 单曲循环
1685500cea7S猫头猫        track = getPlayListMusicAt(currentIndex) as Track;
1695500cea7S猫头猫    } else {
1705500cea7S猫头猫        // 下一曲
1715500cea7S猫头猫        track = getPlayListMusicAt(currentIndex + 1) as Track;
1725500cea7S猫头猫    }
1735500cea7S猫头猫
1745500cea7S猫头猫    if (track) {
1755500cea7S猫头猫        return produce(track, _ => {
1765500cea7S猫头猫            _.url = SoundAsset.fakeAudio;
1775500cea7S猫头猫            _.$ = internalFakeSoundKey;
1785500cea7S猫头猫        });
1795500cea7S猫头猫    } else {
1805500cea7S猫头猫        // 只有列表长度为0时才会出现的特殊情况
1815500cea7S猫头猫        return {url: SoundAsset.fakeAudio, $: internalFakeSoundKey} as Track;
1825500cea7S猫头猫    }
1835500cea7S猫头猫};
1845500cea7S猫头猫
1855500cea7S猫头猫/** 播放失败时的情况 */
1866f57784cS猫头猫async function failToPlay() {
1875500cea7S猫头猫    // 如果自动跳转下一曲, 500s后自动跳转
1885500cea7S猫头猫    if (!Config.get('setting.basic.autoStopWhenError')) {
1895500cea7S猫头猫        await ReactNativeTrackPlayer.reset();
1905500cea7S猫头猫        await delay(500);
1916f57784cS猫头猫        await skipToNext();
1925500cea7S猫头猫    }
1935500cea7S猫头猫}
1945500cea7S猫头猫
1955500cea7S猫头猫// 播放模式相关
1965500cea7S猫头猫const _toggleRepeatMapping = {
1975500cea7S猫头猫    [MusicRepeatMode.SHUFFLE]: MusicRepeatMode.SINGLE,
1985500cea7S猫头猫    [MusicRepeatMode.SINGLE]: MusicRepeatMode.QUEUE,
1995500cea7S猫头猫    [MusicRepeatMode.QUEUE]: MusicRepeatMode.SHUFFLE,
2005500cea7S猫头猫};
2015500cea7S猫头猫/** 切换下一个模式 */
2025500cea7S猫头猫const toggleRepeatMode = () => {
2035500cea7S猫头猫    setRepeatMode(_toggleRepeatMapping[repeatModeStore.getValue()]);
2045500cea7S猫头猫};
2055500cea7S猫头猫
2065500cea7S猫头猫/** 设置音源 */
2075500cea7S猫头猫const setTrackSource = async (track: Track, autoPlay = true) => {
2085500cea7S猫头猫    await ReactNativeTrackPlayer.setQueue([track, getFakeNextTrack()]);
2095500cea7S猫头猫    if (autoPlay) {
2105500cea7S猫头猫        await ReactNativeTrackPlayer.play();
2115500cea7S猫头猫    }
2125500cea7S猫头猫    // 写缓存 TODO: MMKV
2135500cea7S猫头猫    Config.set('status.music.track', track as IMusic.IMusicItem, false);
2145500cea7S猫头猫    Config.set('status.music.progress', 0, false);
2155500cea7S猫头猫};
2165500cea7S猫头猫
2175500cea7S猫头猫/**
2185500cea7S猫头猫 * 添加到播放列表
2195500cea7S猫头猫 * @param musicItems 目标歌曲
2205500cea7S猫头猫 * @param beforeIndex 在第x首歌曲前添加
2215500cea7S猫头猫 * @param shouldShuffle 随机排序
2225500cea7S猫头猫 */
2235500cea7S猫头猫const addAll = (
2245500cea7S猫头猫    musicItems: Array<IMusic.IMusicItem> = [],
2255500cea7S猫头猫    beforeIndex?: number,
2265500cea7S猫头猫    shouldShuffle?: boolean,
2275500cea7S猫头猫) => {
2285500cea7S猫头猫    const now = Date.now();
2295500cea7S猫头猫    let newPlayList: IMusic.IMusicItem[] = [];
2305500cea7S猫头猫    let currentPlayList = getPlayList();
2315500cea7S猫头猫    const _musicItems = musicItems.map((item, index) =>
2325500cea7S猫头猫        produce(item, draft => {
2335500cea7S猫头猫            draft[timeStampSymbol] = now;
2345500cea7S猫头猫            draft[sortIndexSymbol] = index;
2355500cea7S猫头猫        }),
2365500cea7S猫头猫    );
2375500cea7S猫头猫    if (beforeIndex === undefined || beforeIndex < 0) {
2385500cea7S猫头猫        // 1.1. 添加到歌单末尾,并过滤掉已有的歌曲
2395500cea7S猫头猫        newPlayList = currentPlayList.concat(
2405500cea7S猫头猫            _musicItems.filter(item => !isInPlayList(item)),
2415500cea7S猫头猫        );
2425500cea7S猫头猫    } else {
2435500cea7S猫头猫        // 1.2. 新的播放列表,插入
2445500cea7S猫头猫        const indexMap = createMediaIndexMap(_musicItems);
2455500cea7S猫头猫        const beforeDraft = currentPlayList
2465500cea7S猫头猫            .slice(0, beforeIndex)
2475500cea7S猫头猫            .filter(item => !indexMap.has(item));
2485500cea7S猫头猫        const afterDraft = currentPlayList
2495500cea7S猫头猫            .slice(beforeIndex)
2505500cea7S猫头猫            .filter(item => !indexMap.has(item));
2515500cea7S猫头猫
2525500cea7S猫头猫        newPlayList = [...beforeDraft, ..._musicItems, ...afterDraft];
2535500cea7S猫头猫    }
25415900d05S猫头猫
2555500cea7S猫头猫    // 如果太长了
2565500cea7S猫头猫    if (newPlayList.length > maxMusicQueueLength) {
2575500cea7S猫头猫        newPlayList = shrinkPlayListToSize(
2585500cea7S猫头猫            newPlayList,
2595500cea7S猫头猫            beforeIndex ?? newPlayList.length - 1,
2605500cea7S猫头猫        );
2615500cea7S猫头猫    }
2625500cea7S猫头猫
2635500cea7S猫头猫    // 2. 如果需要随机
2645500cea7S猫头猫    if (shouldShuffle) {
2655500cea7S猫头猫        newPlayList = shuffle(newPlayList);
2665500cea7S猫头猫    }
2675500cea7S猫头猫    // 3. 设置播放列表
2685500cea7S猫头猫    setPlayList(newPlayList);
2695500cea7S猫头猫    const currentMusicItem = currentMusicStore.getValue();
2705500cea7S猫头猫
2715500cea7S猫头猫    // 4. 重置下标
2725500cea7S猫头猫    if (currentMusicItem) {
2735500cea7S猫头猫        currentIndex = getMusicIndex(currentMusicItem);
2745500cea7S猫头猫    }
2755500cea7S猫头猫
2765500cea7S猫头猫    // TODO: 更新播放队列信息
2775500cea7S猫头猫    // 5. 存储更新的播放列表信息
2785500cea7S猫头猫};
2795500cea7S猫头猫
2805500cea7S猫头猫/** 追加到队尾 */
2815500cea7S猫头猫const add = (
2825500cea7S猫头猫    musicItem: IMusic.IMusicItem | IMusic.IMusicItem[],
2835500cea7S猫头猫    beforeIndex?: number,
2845500cea7S猫头猫) => {
2855500cea7S猫头猫    addAll(Array.isArray(musicItem) ? musicItem : [musicItem], beforeIndex);
2865500cea7S猫头猫};
2875500cea7S猫头猫
2885500cea7S猫头猫/**
2895500cea7S猫头猫 * 下一首播放
2905500cea7S猫头猫 * @param musicItem
2915500cea7S猫头猫 */
2925500cea7S猫头猫const addNext = (musicItem: IMusic.IMusicItem | IMusic.IMusicItem[]) => {
2935500cea7S猫头猫    const shouldPlay = isPlayListEmpty();
2945500cea7S猫头猫    add(musicItem, currentIndex + 1);
2955500cea7S猫头猫    if (shouldPlay) {
2965500cea7S猫头猫        play(Array.isArray(musicItem) ? musicItem[0] : musicItem);
2975500cea7S猫头猫    }
2985500cea7S猫头猫};
2995500cea7S猫头猫
3005500cea7S猫头猫const isCurrentMusic = (musicItem: IMusic.IMusicItem) => {
3015500cea7S猫头猫    return isSameMediaItem(musicItem, currentMusicStore.getValue()) ?? false;
3025500cea7S猫头猫};
3035500cea7S猫头猫
3045500cea7S猫头猫const remove = async (musicItem: IMusic.IMusicItem) => {
3055500cea7S猫头猫    const playList = getPlayList();
3065500cea7S猫头猫    let newPlayList: IMusic.IMusicItem[] = [];
3075500cea7S猫头猫    let currentMusic: IMusic.IMusicItem | null = currentMusicStore.getValue();
3085500cea7S猫头猫    const targetIndex = getMusicIndex(musicItem);
3095500cea7S猫头猫    let shouldPlayCurrent: boolean | null = null;
3105500cea7S猫头猫    if (targetIndex === -1) {
3115500cea7S猫头猫        // 1. 这种情况应该是出错了
3125500cea7S猫头猫        return;
3135500cea7S猫头猫    }
3145500cea7S猫头猫    // 2. 移除的是当前项
3155500cea7S猫头猫    if (currentIndex === targetIndex) {
3165500cea7S猫头猫        // 2.1 停止播放,移除当前项
3175500cea7S猫头猫        newPlayList = produce(playList, draft => {
3185500cea7S猫头猫            draft.splice(targetIndex, 1);
3195500cea7S猫头猫        });
3205500cea7S猫头猫        // 2.2 设置新的播放列表,并更新当前音乐
3215500cea7S猫头猫        if (newPlayList.length === 0) {
3225500cea7S猫头猫            currentMusic = null;
3235500cea7S猫头猫            shouldPlayCurrent = false;
3245500cea7S猫头猫        } else {
3255500cea7S猫头猫            currentMusic = newPlayList[currentIndex % newPlayList.length];
3265500cea7S猫头猫            try {
3275500cea7S猫头猫                const state = (await ReactNativeTrackPlayer.getPlaybackState())
3285500cea7S猫头猫                    .state;
3295500cea7S猫头猫                if (musicIsPaused(state)) {
3305500cea7S猫头猫                    shouldPlayCurrent = false;
3315500cea7S猫头猫                } else {
3325500cea7S猫头猫                    shouldPlayCurrent = true;
3335500cea7S猫头猫                }
3345500cea7S猫头猫            } catch {
3355500cea7S猫头猫                shouldPlayCurrent = false;
3365500cea7S猫头猫            }
3375500cea7S猫头猫        }
3385500cea7S猫头猫    } else {
3395500cea7S猫头猫        // 3. 删除
3405500cea7S猫头猫        newPlayList = produce(playList, draft => {
3415500cea7S猫头猫            draft.splice(targetIndex, 1);
3425500cea7S猫头猫        });
3435500cea7S猫头猫    }
3445500cea7S猫头猫
3455500cea7S猫头猫    setPlayList(newPlayList);
3465500cea7S猫头猫    setCurrentMusic(currentMusic);
3475500cea7S猫头猫    Config.set('status.music.musicQueue', playList, false);
3485500cea7S猫头猫    if (shouldPlayCurrent === true) {
3495500cea7S猫头猫        await play(currentMusic, true);
3505500cea7S猫头猫    } else if (shouldPlayCurrent === false) {
3515500cea7S猫头猫        await ReactNativeTrackPlayer.reset();
3525500cea7S猫头猫    }
3535500cea7S猫头猫};
3545500cea7S猫头猫
3555500cea7S猫头猫/**
3565500cea7S猫头猫 * 设置播放模式
3575500cea7S猫头猫 * @param mode 播放模式
3585500cea7S猫头猫 */
3595500cea7S猫头猫const setRepeatMode = (mode: MusicRepeatMode) => {
3605500cea7S猫头猫    const playList = getPlayList();
3615500cea7S猫头猫    let newPlayList;
3625500cea7S猫头猫    if (mode === MusicRepeatMode.SHUFFLE) {
3635500cea7S猫头猫        newPlayList = shuffle(playList);
3645500cea7S猫头猫    } else {
3655500cea7S猫头猫        newPlayList = produce(playList, draft => {
3665500cea7S猫头猫            return sortByTimestampAndIndex(draft);
3675500cea7S猫头猫        });
3685500cea7S猫头猫    }
3695500cea7S猫头猫
3705500cea7S猫头猫    setPlayList(newPlayList);
3715500cea7S猫头猫    const currentMusicItem = currentMusicStore.getValue();
3725500cea7S猫头猫    currentIndex = getMusicIndex(currentMusicItem);
3735500cea7S猫头猫    repeatModeStore.setValue(mode);
3745500cea7S猫头猫    // 更新下一首歌的信息
3755500cea7S猫头猫    ReactNativeTrackPlayer.updateMetadataForTrack(1, getFakeNextTrack());
3765500cea7S猫头猫    // 记录
3775500cea7S猫头猫    Config.set('status.music.repeatMode', mode, false);
3785500cea7S猫头猫};
3795500cea7S猫头猫
3805500cea7S猫头猫/** 清空播放列表 */
3815500cea7S猫头猫const clear = async () => {
3825500cea7S猫头猫    setPlayList([]);
3835500cea7S猫头猫    setCurrentMusic(null);
3845500cea7S猫头猫
3855500cea7S猫头猫    await ReactNativeTrackPlayer.reset();
3865500cea7S猫头猫    Config.set('status.music', {
3875500cea7S猫头猫        repeatMode: repeatModeStore.getValue(),
3885500cea7S猫头猫    });
3895500cea7S猫头猫};
3905500cea7S猫头猫
3915500cea7S猫头猫/** 暂停 */
3925500cea7S猫头猫const pause = async () => {
3935500cea7S猫头猫    await ReactNativeTrackPlayer.pause();
3945500cea7S猫头猫};
3955500cea7S猫头猫
3965500cea7S猫头猫const setCurrentMusic = (musicItem?: IMusic.IMusicItem | null) => {
3975500cea7S猫头猫    if (!musicItem) {
3985500cea7S猫头猫        currentIndex = -1;
399f511aee9S猫头猫        currentMusicStore.setValue(null);
4005500cea7S猫头猫    }
4015500cea7S猫头猫    currentIndex = getMusicIndex(musicItem);
402f511aee9S猫头猫    currentMusicStore.setValue(musicItem!);
4035500cea7S猫头猫};
4045500cea7S猫头猫
4055500cea7S猫头猫/**
4065500cea7S猫头猫 * 播放
4075500cea7S猫头猫 *
4085500cea7S猫头猫 * 当musicItem 为空时,代表暂停/播放
4095500cea7S猫头猫 *
4105500cea7S猫头猫 * @param musicItem
4115500cea7S猫头猫 * @param forcePlay
4125500cea7S猫头猫 * @returns
4135500cea7S猫头猫 */
4145500cea7S猫头猫const play = async (
4155500cea7S猫头猫    musicItem?: IMusic.IMusicItem | null,
4165500cea7S猫头猫    forcePlay?: boolean,
4175500cea7S猫头猫) => {
4185500cea7S猫头猫    try {
4195500cea7S猫头猫        if (!musicItem) {
4205500cea7S猫头猫            musicItem = currentMusicStore.getValue();
4215500cea7S猫头猫        }
4225500cea7S猫头猫        if (!musicItem) {
4235500cea7S猫头猫            throw new Error(PlayFailReason.PLAY_LIST_IS_EMPTY);
4245500cea7S猫头猫        }
4255500cea7S猫头猫        // 1. 移动网络禁止播放
4265500cea7S猫头猫        if (
4275500cea7S猫头猫            Network.isCellular() &&
4285500cea7S猫头猫            !Config.get('setting.basic.useCelluarNetworkPlay') &&
4295500cea7S猫头猫            !LocalMusicSheet.isLocalMusic(musicItem)
4305500cea7S猫头猫        ) {
4315500cea7S猫头猫            await ReactNativeTrackPlayer.reset();
4325500cea7S猫头猫            throw new Error(PlayFailReason.FORBID_CELLUAR_NETWORK_PLAY);
4335500cea7S猫头猫        }
4345500cea7S猫头猫
4355500cea7S猫头猫        // 2. 如果是当前正在播放的音频
4365500cea7S猫头猫        if (isCurrentMusic(musicItem)) {
4375500cea7S猫头猫            const currentTrack = await ReactNativeTrackPlayer.getTrack(0);
4385500cea7S猫头猫            // 2.1 如果当前有源
4395500cea7S猫头猫            if (
4405500cea7S猫头猫                currentTrack?.url &&
4415500cea7S猫头猫                isSameMediaItem(musicItem, currentTrack as IMusic.IMusicItem)
4425500cea7S猫头猫            ) {
4435500cea7S猫头猫                const currentActiveIndex =
4445500cea7S猫头猫                    await ReactNativeTrackPlayer.getActiveTrackIndex();
4455500cea7S猫头猫                if (currentActiveIndex !== 0) {
4465500cea7S猫头猫                    await ReactNativeTrackPlayer.skip(0);
4475500cea7S猫头猫                }
4485500cea7S猫头猫                if (forcePlay) {
4495500cea7S猫头猫                    // 2.1.1 强制重新开始
4505500cea7S猫头猫                    await ReactNativeTrackPlayer.seekTo(0);
4516f57784cS猫头猫                }
4526f57784cS猫头猫                if (
4535500cea7S猫头猫                    (await ReactNativeTrackPlayer.getPlaybackState()).state !==
4545500cea7S猫头猫                    State.Playing
4555500cea7S猫头猫                ) {
4565500cea7S猫头猫                    // 2.1.2 恢复播放
4575500cea7S猫头猫                    await ReactNativeTrackPlayer.play();
4585500cea7S猫头猫                }
4595500cea7S猫头猫                // 这种情况下,播放队列和当前歌曲都不需要变化
4605500cea7S猫头猫                return;
4615500cea7S猫头猫            }
4625500cea7S猫头猫            // 2.2 其他情况:重新获取源
4635500cea7S猫头猫        }
4645500cea7S猫头猫
4655500cea7S猫头猫        // 3. 如果没有在播放列表中,添加到队尾;同时更新列表状态
4665500cea7S猫头猫        const inPlayList = isInPlayList(musicItem);
4675500cea7S猫头猫        if (!inPlayList) {
4685500cea7S猫头猫            add(musicItem);
4695500cea7S猫头猫        }
4705500cea7S猫头猫
4715500cea7S猫头猫        // 4. 更新列表状态和当前音乐
4725500cea7S猫头猫        setCurrentMusic(musicItem);
4735500cea7S猫头猫
4745500cea7S猫头猫        // 5. 获取音源
4755500cea7S猫头猫        let track: IMusic.IMusicItem;
4765500cea7S猫头猫
4775500cea7S猫头猫        // 5.1 通过插件获取音源
4785500cea7S猫头猫        const plugin = PluginManager.getByName(musicItem.platform);
4795500cea7S猫头猫        // 5.2 获取音质排序
4805500cea7S猫头猫        const qualityOrder = getQualityOrder(
4815500cea7S猫头猫            Config.get('setting.basic.defaultPlayQuality') ?? 'standard',
4825500cea7S猫头猫            Config.get('setting.basic.playQualityOrder') ?? 'asc',
4835500cea7S猫头猫        );
4845500cea7S猫头猫        // 5.3 插件返回音源
4855500cea7S猫头猫        let source: IPlugin.IMediaSourceResult | null = null;
4865500cea7S猫头猫        for (let quality of qualityOrder) {
4875500cea7S猫头猫            if (isCurrentMusic(musicItem)) {
4885500cea7S猫头猫                source =
4895500cea7S猫头猫                    (await plugin?.methods?.getMediaSource(
4905500cea7S猫头猫                        musicItem,
4915500cea7S猫头猫                        quality,
4925500cea7S猫头猫                    )) ?? null;
4935500cea7S猫头猫                // 5.3.1 获取到真实源
4945500cea7S猫头猫                if (source) {
4955500cea7S猫头猫                    qualityStore.setValue(quality);
4965500cea7S猫头猫                    break;
4975500cea7S猫头猫                }
4985500cea7S猫头猫            } else {
4995500cea7S猫头猫                // 5.3.2 已经切换到其他歌曲了,
5005500cea7S猫头猫                return;
5015500cea7S猫头猫            }
5025500cea7S猫头猫        }
5035500cea7S猫头猫
5045500cea7S猫头猫        if (!isCurrentMusic(musicItem)) {
5055500cea7S猫头猫            return;
5065500cea7S猫头猫        }
5075500cea7S猫头猫
5085500cea7S猫头猫        if (!source) {
509*43eb30bfS猫头猫            // 如果有source
510*43eb30bfS猫头猫            if (musicItem.source) {
511*43eb30bfS猫头猫                for (let quality of qualityOrder) {
512*43eb30bfS猫头猫                    if (musicItem.source[quality]?.url) {
513*43eb30bfS猫头猫                        source = musicItem.source[quality]!;
514*43eb30bfS猫头猫                        qualityStore.setValue(quality);
515*43eb30bfS猫头猫                        break;
5165500cea7S猫头猫                    }
517*43eb30bfS猫头猫                }
518*43eb30bfS猫头猫            }
519*43eb30bfS猫头猫
520*43eb30bfS猫头猫            // 5.4 没有返回源
521*43eb30bfS猫头猫            if (!source && !musicItem.url) {
522*43eb30bfS猫头猫                throw new Error(PlayFailReason.INVALID_SOURCE);
523*43eb30bfS猫头猫            } else {
5245500cea7S猫头猫                source = {
5255500cea7S猫头猫                    url: musicItem.url,
5265500cea7S猫头猫                };
5275500cea7S猫头猫                qualityStore.setValue('standard');
5285500cea7S猫头猫            }
529*43eb30bfS猫头猫        }
5305500cea7S猫头猫
5315500cea7S猫头猫        // 6. 特殊类型源
5325500cea7S猫头猫        if (getUrlExt(source.url) === '.m3u8') {
5335500cea7S猫头猫            // @ts-ignore
5345500cea7S猫头猫            source.type = 'hls';
5355500cea7S猫头猫        }
5365500cea7S猫头猫        // 7. 合并结果
5375500cea7S猫头猫        track = mergeProps(musicItem, source) as IMusic.IMusicItem;
5385500cea7S猫头猫
5395500cea7S猫头猫        // 8. 新增历史记录
5405500cea7S猫头猫        musicHistory.addMusic(musicItem);
5415500cea7S猫头猫
54262e73a5eS猫头猫        trace('获取音源成功', track);
54362e73a5eS猫头猫
5445500cea7S猫头猫        // 9. 设置音源
5455500cea7S猫头猫        await setTrackSource(track as Track);
5465500cea7S猫头猫
5475500cea7S猫头猫        // 10. 获取补充信息
5485500cea7S猫头猫        let info: Partial<IMusic.IMusicItem> | null = null;
5495500cea7S猫头猫        try {
5505500cea7S猫头猫            info = (await plugin?.methods?.getMusicInfo?.(musicItem)) ?? null;
5515500cea7S猫头猫        } catch {}
5525500cea7S猫头猫
5535500cea7S猫头猫        // 11. 设置补充信息
5545500cea7S猫头猫        if (info && isCurrentMusic(musicItem)) {
5555500cea7S猫头猫            const mergedTrack = mergeProps(track, info);
5565500cea7S猫头猫            currentMusicStore.setValue(mergedTrack as IMusic.IMusicItem);
5575500cea7S猫头猫            await ReactNativeTrackPlayer.updateMetadataForTrack(
5585500cea7S猫头猫                0,
5595500cea7S猫头猫                mergedTrack as TrackMetadataBase,
5605500cea7S猫头猫            );
5615500cea7S猫头猫        }
5625500cea7S猫头猫
5635500cea7S猫头猫        // 12. 刷新歌词信息
5645500cea7S猫头猫        if (
5655500cea7S猫头猫            !isSameMediaItem(
5665500cea7S猫头猫                LyricManager.getLyricState()?.lyricParser?.getCurrentMusicItem?.(),
5675500cea7S猫头猫                musicItem,
5685500cea7S猫头猫            )
5695500cea7S猫头猫        ) {
5705500cea7S猫头猫            DeviceEventEmitter.emit(EDeviceEvents.REFRESH_LYRIC, true);
5715500cea7S猫头猫        }
5725500cea7S猫头猫    } catch (e: any) {
5735500cea7S猫头猫        const message = e?.message;
5745500cea7S猫头猫        if (
5755500cea7S猫头猫            message === 'The player is not initialized. Call setupPlayer first.'
5765500cea7S猫头猫        ) {
5775500cea7S猫头猫            await ReactNativeTrackPlayer.setupPlayer();
5785500cea7S猫头猫            play(musicItem, forcePlay);
5795500cea7S猫头猫        } else if (message === PlayFailReason.FORBID_CELLUAR_NETWORK_PLAY) {
580f511aee9S猫头猫            Toast.warn(
581f511aee9S猫头猫                '当前禁止移动网络播放音乐,如需播放请去侧边栏-基本设置中修改',
582f511aee9S猫头猫            );
5835500cea7S猫头猫        } else if (message === PlayFailReason.INVALID_SOURCE) {
58462e73a5eS猫头猫            trace('音源为空,播放失败');
5856f57784cS猫头猫            await failToPlay();
5865500cea7S猫头猫        } else if (message === PlayFailReason.PLAY_LIST_IS_EMPTY) {
5875500cea7S猫头猫            // 队列是空的,不应该出现这种情况
5885500cea7S猫头猫        }
5895500cea7S猫头猫    }
5905500cea7S猫头猫};
5915500cea7S猫头猫
5925500cea7S猫头猫/**
5935500cea7S猫头猫 * 播放音乐,同时替换播放队列
5945500cea7S猫头猫 * @param musicItem 音乐
5955500cea7S猫头猫 * @param newPlayList 替代列表
5965500cea7S猫头猫 */
5975500cea7S猫头猫const playWithReplacePlayList = async (
5985500cea7S猫头猫    musicItem: IMusic.IMusicItem,
5995500cea7S猫头猫    newPlayList: IMusic.IMusicItem[],
6005500cea7S猫头猫) => {
6015500cea7S猫头猫    if (newPlayList.length !== 0) {
6025500cea7S猫头猫        const now = Date.now();
6035500cea7S猫头猫        if (newPlayList.length > maxMusicQueueLength) {
6045500cea7S猫头猫            newPlayList = shrinkPlayListToSize(
6055500cea7S猫头猫                newPlayList,
6065500cea7S猫头猫                newPlayList.findIndex(it => isSameMediaItem(it, musicItem)),
6075500cea7S猫头猫            );
6085500cea7S猫头猫        }
6095500cea7S猫头猫        const playListItems = newPlayList.map((item, index) =>
6105500cea7S猫头猫            produce(item, draft => {
6115500cea7S猫头猫                draft[timeStampSymbol] = now;
6125500cea7S猫头猫                draft[sortIndexSymbol] = index;
6135500cea7S猫头猫            }),
6145500cea7S猫头猫        );
6155500cea7S猫头猫        setPlayList(
6165500cea7S猫头猫            repeatModeStore.getValue() === MusicRepeatMode.SHUFFLE
6175500cea7S猫头猫                ? shuffle(playListItems)
6185500cea7S猫头猫                : playListItems,
6195500cea7S猫头猫        );
6205500cea7S猫头猫        await play(musicItem, true);
6215500cea7S猫头猫    }
6225500cea7S猫头猫};
6235500cea7S猫头猫
6246f57784cS猫头猫const skipToNext = async () => {
6255500cea7S猫头猫    if (isPlayListEmpty()) {
6265500cea7S猫头猫        setCurrentMusic(null);
6275500cea7S猫头猫        return;
6285500cea7S猫头猫    }
6295500cea7S猫头猫
6305500cea7S猫头猫    await play(getPlayListMusicAt(currentIndex + 1), true);
6315500cea7S猫头猫};
6325500cea7S猫头猫
6335500cea7S猫头猫const skipToPrevious = async () => {
6345500cea7S猫头猫    if (isPlayListEmpty()) {
6355500cea7S猫头猫        setCurrentMusic(null);
6365500cea7S猫头猫        return;
6375500cea7S猫头猫    }
6385500cea7S猫头猫
6396f57784cS猫头猫    await play(
6406f57784cS猫头猫        getPlayListMusicAt(currentIndex === -1 ? 0 : currentIndex - 1),
6416f57784cS猫头猫        true,
6426f57784cS猫头猫    );
6435500cea7S猫头猫};
6445500cea7S猫头猫
6455500cea7S猫头猫/** 修改当前播放的音质 */
6465500cea7S猫头猫const changeQuality = async (newQuality: IMusic.IQualityKey) => {
6475500cea7S猫头猫    // 获取当前的音乐和进度
6485500cea7S猫头猫    if (newQuality === qualityStore.getValue()) {
6495500cea7S猫头猫        return true;
6505500cea7S猫头猫    }
6515500cea7S猫头猫
6525500cea7S猫头猫    // 获取当前歌曲
6535500cea7S猫头猫    const musicItem = currentMusicStore.getValue();
6545500cea7S猫头猫    if (!musicItem) {
6555500cea7S猫头猫        return false;
6565500cea7S猫头猫    }
6575500cea7S猫头猫    try {
6585500cea7S猫头猫        const progress = await ReactNativeTrackPlayer.getProgress();
6595500cea7S猫头猫        const plugin = PluginManager.getByMedia(musicItem);
6605500cea7S猫头猫        const newSource = await plugin?.methods?.getMediaSource(
6615500cea7S猫头猫            musicItem,
6625500cea7S猫头猫            newQuality,
6635500cea7S猫头猫        );
6645500cea7S猫头猫        if (!newSource?.url) {
6655500cea7S猫头猫            throw new Error(PlayFailReason.INVALID_SOURCE);
6665500cea7S猫头猫        }
6675500cea7S猫头猫        if (isCurrentMusic(musicItem)) {
6685500cea7S猫头猫            const playingState = (
6695500cea7S猫头猫                await ReactNativeTrackPlayer.getPlaybackState()
6705500cea7S猫头猫            ).state;
6715500cea7S猫头猫            await setTrackSource(
6725500cea7S猫头猫                mergeProps(musicItem, newSource) as unknown as Track,
6735500cea7S猫头猫                !musicIsPaused(playingState),
6745500cea7S猫头猫            );
6755500cea7S猫头猫
6765500cea7S猫头猫            await ReactNativeTrackPlayer.seekTo(progress.position ?? 0);
6775500cea7S猫头猫            qualityStore.setValue(newQuality);
6785500cea7S猫头猫        }
6795500cea7S猫头猫        return true;
6805500cea7S猫头猫    } catch {
6815500cea7S猫头猫        // 修改失败
6825500cea7S猫头猫        return false;
6835500cea7S猫头猫    }
6845500cea7S猫头猫};
6855500cea7S猫头猫
6865500cea7S猫头猫enum PlayFailReason {
6875500cea7S猫头猫    /** 禁止移动网络播放 */
6885500cea7S猫头猫    FORBID_CELLUAR_NETWORK_PLAY = 'FORBID_CELLUAR_NETWORK_PLAY',
6895500cea7S猫头猫    /** 播放列表为空 */
6905500cea7S猫头猫    PLAY_LIST_IS_EMPTY = 'PLAY_LIST_IS_EMPTY',
6915500cea7S猫头猫    /** 无效源 */
6925500cea7S猫头猫    INVALID_SOURCE = 'INVALID_SOURCE',
6935500cea7S猫头猫    /** 非当前音乐 */
6945500cea7S猫头猫}
6955500cea7S猫头猫
6965500cea7S猫头猫function useMusicState() {
6975500cea7S猫头猫    const playbackState = usePlaybackState();
6985500cea7S猫头猫
6995500cea7S猫头猫    return playbackState.state;
7005500cea7S猫头猫}
7015500cea7S猫头猫
702f511aee9S猫头猫function getPreviousMusic() {
703f511aee9S猫头猫    const currentMusicItem = currentMusicStore.getValue();
704f511aee9S猫头猫    if (!currentMusicItem) {
705f511aee9S猫头猫        return null;
706f511aee9S猫头猫    }
707f511aee9S猫头猫
708f511aee9S猫头猫    return getPlayListMusicAt(currentIndex - 1);
709f511aee9S猫头猫}
710f511aee9S猫头猫
711f511aee9S猫头猫function getNextMusic() {
712f511aee9S猫头猫    const currentMusicItem = currentMusicStore.getValue();
713f511aee9S猫头猫    if (!currentMusicItem) {
714f511aee9S猫头猫        return null;
715f511aee9S猫头猫    }
716f511aee9S猫头猫
717f511aee9S猫头猫    return getPlayListMusicAt(currentIndex + 1);
718f511aee9S猫头猫}
719f511aee9S猫头猫
7205500cea7S猫头猫const TrackPlayer = {
7215500cea7S猫头猫    setupTrackPlayer,
7225500cea7S猫头猫    usePlayList,
7235500cea7S猫头猫    getPlayList,
7245500cea7S猫头猫    addAll,
7255500cea7S猫头猫    add,
7265500cea7S猫头猫    addNext,
7275500cea7S猫头猫    skipToNext,
7285500cea7S猫头猫    skipToPrevious,
7295500cea7S猫头猫    play,
7305500cea7S猫头猫    playWithReplacePlayList,
7315500cea7S猫头猫    pause,
7325500cea7S猫头猫    remove,
7335500cea7S猫头猫    clear,
7345500cea7S猫头猫    useCurrentMusic: currentMusicStore.useValue,
7355500cea7S猫头猫    getCurrentMusic: currentMusicStore.getValue,
7365500cea7S猫头猫    useRepeatMode: repeatModeStore.useValue,
7375500cea7S猫头猫    getRepeatMode: repeatModeStore.getValue,
7385500cea7S猫头猫    toggleRepeatMode,
7395500cea7S猫头猫    usePlaybackState,
7405500cea7S猫头猫    getProgress: ReactNativeTrackPlayer.getProgress,
7415500cea7S猫头猫    useProgress: useProgress,
7425500cea7S猫头猫    seekTo: ReactNativeTrackPlayer.seekTo,
7435500cea7S猫头猫    changeQuality,
7445500cea7S猫头猫    useCurrentQuality: qualityStore.useValue,
7455500cea7S猫头猫    getCurrentQuality: qualityStore.getValue,
7465500cea7S猫头猫    getRate: ReactNativeTrackPlayer.getRate,
7475500cea7S猫头猫    setRate: ReactNativeTrackPlayer.setRate,
7485500cea7S猫头猫    useMusicState,
7495500cea7S猫头猫    reset: ReactNativeTrackPlayer.reset,
750f511aee9S猫头猫    getPreviousMusic,
751f511aee9S猫头猫    getNextMusic,
7525500cea7S猫头猫};
7535500cea7S猫头猫
7545500cea7S猫头猫export default TrackPlayer;
7555500cea7S猫头猫export {MusicRepeatMode, State as MusicState};
756