52 lines
1.3 KiB
TypeScript
52 lines
1.3 KiB
TypeScript
import { useAudioQuality } from '~/composables/useAudioQuality'
|
|
import { useMusicSources } from '~/composables/useMusicSources'
|
|
|
|
/**
|
|
* 动态获取音乐播放URL
|
|
* @param platform 音乐平台
|
|
* @param musicId 音乐ID
|
|
* @param playUrl 用户提供的播放链接(可选)
|
|
* @param options 额外选项,例如 { unblock: boolean, quality: number }
|
|
* @returns Promise<string | null> 返回播放URL或null
|
|
*/
|
|
export async function getMusicUrl(
|
|
platform: string,
|
|
musicId: string | number,
|
|
playUrl?: string,
|
|
options?: { unblock?: boolean; quality?: number }
|
|
): Promise<string | null> {
|
|
if (playUrl && playUrl.trim()) {
|
|
return playUrl.trim()
|
|
}
|
|
|
|
if (
|
|
!platform ||
|
|
!musicId ||
|
|
platform === 'unknown' ||
|
|
platform === '' ||
|
|
musicId === null ||
|
|
musicId === ''
|
|
) {
|
|
throw new Error('缺少音乐平台或音乐ID信息')
|
|
}
|
|
|
|
const { getQuality } = useAudioQuality()
|
|
const { getSongUrl } = useMusicSources()
|
|
|
|
const quality =
|
|
options?.quality !== undefined ? options.quality : getQuality(platform)
|
|
|
|
const result = await getSongUrl(
|
|
musicId,
|
|
quality,
|
|
platform,
|
|
undefined,
|
|
options
|
|
)
|
|
if (result.success && result.url) {
|
|
return result.url
|
|
}
|
|
|
|
throw new Error(result.error || '获取音乐播放链接失败')
|
|
}
|