Skip to content

Commit

Permalink
Scheme URL API新增 music/searchPlay 支持(#1886
Browse files Browse the repository at this point in the history
  • Loading branch information
lyswhut committed May 14, 2024
1 parent 8e7fd1e commit 2ee7572
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
1 change: 1 addition & 0 deletions publish/changeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- 新增 设置-播放设置-使用设备能处理的最大声道数输出音频 设置(未启用时固定为2声道输出),由于这用到高级音频API,考虑到在某些设备上的兼容问题,默认禁用(#1873
- 允许添加 `m4a``oga` 格式的本地歌曲到列表中(#1864
- 开放API支持跨域请求(#1872 @Ceale
- Scheme URL API新增 `music/searchPlay` 支持,用于搜索并播放指定的歌曲名字,详细入参请阅读 Scheme URL 支持文档(#1886

### 优化

Expand Down
2 changes: 1 addition & 1 deletion src/renderer/core/music/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const getOtherSourcePromises = new Map()
export const existTimeExp = /\[\d{1,2}:.*\d{1,4}\]/

export const getOtherSource = async(musicInfo: LX.Music.MusicInfo | LX.Download.ListItem, isRefresh = false): Promise<LX.Music.MusicInfoOnline[]> => {
if (!isRefresh) {
if (!isRefresh && musicInfo.id) {
const cachedInfo = await getOtherSourceFromStore(musicInfo.id)
if (cachedInfo.length) return cachedInfo
}
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/core/useApp/useDeeplink/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default () => {
console.log(params)
switch (type) {
case 'music':
handleMusicAction(action, params)
await handleMusicAction(action, params)
break
case 'songlist':
await handleSonglistAction(action, params)
Expand Down
63 changes: 62 additions & 1 deletion src/renderer/core/useApp/useDeeplink/useMusicAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { focusWindow } from '@renderer/utils/ipc'
import { playNext } from '@renderer/core/player/action'
import { toNewMusicInfo } from '@common/utils/tools'
import { LIST_IDS } from '@common/constants'
import { getOtherSource } from '@renderer/core/music/utils'

const useSearchMusic = () => {
const router = useRouter()
Expand Down Expand Up @@ -159,18 +160,78 @@ const usePlayMusic = () => {
}
}


const useSearchPlayMusic = () => {
const verifyInfo = (info) => {
return dataVerify([
{ key: 'name', types: ['string'], required: true, max: 200 },
{ key: 'singer', types: ['string'], max: 200 },
{ key: 'albumName', types: ['string'], max: 64 },
{ key: 'interval', types: ['string'], max: 64 },
{ key: 'playLater', types: ['boolean'] },
], info)
}

const searchMusic = async(name, singer, albumName, interval) => {
return getOtherSource({
name,
singer,
interval,
meta: {
albumName,
},
source: 'local',
id: `sp_${name}_s${singer}_a${albumName}_i${interval ?? ''}`,
})
}
return async({ paths, data }) => {
// console.log(paths, data)
let info
if (paths.length) {
let name = paths[0]
let singer = ''
if (name.includes('-')) [name, singer] = name.split('-').map(val => val.trim())
info = {
name,
singer,
}
} else info = data
info = verifyInfo(info)
const musicList = await searchMusic(info.name, info.singer || '', info.albumName || '', info.interval || null)
if (musicList.length) {
console.log('find music:', musicList)
const musicInfo = musicList[0]
markRaw(musicInfo)
const isPlaying = !!playMusicInfo.musicInfo
if (info.playLater) {
addTempPlayList([{ listId: LIST_IDS.PLAY_LATER, musicInfo }])
} else {
addTempPlayList([{ listId: LIST_IDS.PLAY_LATER, musicInfo, isTop: true }])
if (isPlaying) playNext()
}
} else {
console.log('msuic not found:', info)
}
}
}

export default () => {
const handleSearchMusic = useSearchMusic()
const handlePlayMusic = usePlayMusic()
const handleSearchPlayMusic = useSearchPlayMusic()


return (action, info) => {
return async(action, info) => {
switch (action) {
case 'search':
handleSearchMusic(info)
break
case 'play':
handlePlayMusic(info)
break
case 'searchPlay':
await handleSearchPlayMusic(info)
break
default: throw new Error('Unknown action: ' + action)
}
}
Expand Down

0 comments on commit 2ee7572

Please sign in to comment.