-
Notifications
You must be signed in to change notification settings - Fork 7k
Expand file tree
/
Copy pathaction.ts
More file actions
266 lines (235 loc) · 7.07 KB
/
Copy pathaction.ts
File metadata and controls
266 lines (235 loc) · 7.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// import { reactive, ref, shallowRef } from '@common/utils/vueTools'
import {
type PlayerMusicInfo,
musicInfo,
isPlay,
status,
statusText,
isShowPlayerDetail,
isShowPlayComment,
isShowLrcSelectContent,
playInfo,
playMusicInfo,
playedList,
tempPlayList,
} from './state'
import { appSetting } from '@renderer/store/setting'
import { getListMusicsFromCache } from '@renderer/store/list/action'
import { downloadList } from '@renderer/store/download/state'
import { setProgress } from './playProgress'
import { playNext } from '@renderer/core/player'
import { getPlayQuality } from '@renderer/core/music/utils'
import { LIST_IDS } from '@common/constants'
import { toRaw } from '@common/utils/vueTools'
import { arrPush, arrUnshift } from '@common/utils/common'
type PlayerMusicInfoKeys = keyof typeof musicInfo
const musicInfoKeys: PlayerMusicInfoKeys[] = Object.keys(musicInfo) as PlayerMusicInfoKeys[]
export const setMusicInfo = (_musicInfo: Partial<PlayerMusicInfo>) => {
for (const key of musicInfoKeys) {
const val = _musicInfo[key]
if (val !== undefined) {
// @ts-expect-error
musicInfo[key] = val
}
}
}
export const setPlay = (val: boolean) => {
isPlay.value = val
}
export const setStatus = (val: string) => {
console.log('setStatus', val)
status.value = val
}
export const setStatusText = (val: string) => {
statusText.value = val
}
export const setAllStatus = (val: string) => {
console.log('setAllStatus', val)
status.value = val
statusText.value = val
}
export const setShowPlayerDetail = (val: boolean) => {
isShowPlayerDetail.value = val
}
export const setShowPlayComment = (val: boolean) => {
isShowPlayComment.value = val
}
export const setShowPlayLrcSelectContentLrc = (val: boolean) => {
isShowLrcSelectContent.value = val
}
export const setPlayListId = (listId: string | null) => {
playInfo.playerListId = listId
}
export const getList = (listId: string | null): Array<LX.Music.MusicInfo | LX.Download.ListItem> => {
return listId == LIST_IDS.DOWNLOAD ? downloadList : getListMusicsFromCache(listId)
}
/**
* 更新播放位置
* @returns 播放位置
*/
export const updatePlayIndex = () => {
const indexInfo = getPlayIndex(playMusicInfo.listId, playMusicInfo.musicInfo, playMusicInfo.isTempPlay)
// console.log(indexInfo)
playInfo.playIndex = indexInfo.playIndex
playInfo.playerPlayIndex = indexInfo.playerPlayIndex
return indexInfo
}
export const getPlayIndex = (listId: string | null, musicInfo: LX.Download.ListItem | LX.Music.MusicInfo | null, isTempPlay: boolean): {
playIndex: number
playerPlayIndex: number
} => {
const playerList = getList(playInfo.playerListId)
// if (listIndex < 0) throw new Error('music info not found')
// playInfo.playIndex = listIndex
let playIndex = -1
let playerPlayIndex = -1
if (playerList.length) {
playerPlayIndex = Math.min(playInfo.playerPlayIndex, playerList.length - 1)
}
const list = getList(listId)
if (list.length && musicInfo) {
const currentId = musicInfo.id
playIndex = list.findIndex(m => m.id == currentId)
if (!isTempPlay) {
if (playIndex < 0) {
playerPlayIndex = playerPlayIndex < 1 ? (list.length - 1) : (playerPlayIndex - 1)
} else {
playerPlayIndex = playIndex
}
}
}
return {
playIndex,
playerPlayIndex,
}
}
export const resetPlayerMusicInfo = () => {
setMusicInfo({
id: null,
pic: null,
lrc: null,
tlrc: null,
rlrc: null,
lxlrc: null,
rawlrc: null,
name: '',
singer: '',
album: '',
})
}
const setPlayerMusicInfo = (musicInfo: LX.Music.MusicInfo | LX.Download.ListItem | null) => {
if (musicInfo) {
setMusicInfo('progress' in musicInfo ? {
id: musicInfo.id,
pic: musicInfo.metadata.musicInfo.meta.picUrl,
name: musicInfo.metadata.musicInfo.name,
singer: musicInfo.metadata.musicInfo.singer,
album: musicInfo.metadata.musicInfo.meta.albumName ?? '',
lrc: null,
tlrc: null,
rlrc: null,
lxlrc: null,
rawlrc: null,
} : {
id: musicInfo.id,
pic: musicInfo.meta.picUrl,
name: musicInfo.name,
singer: musicInfo.singer,
album: musicInfo.meta.albumName ?? '',
lrc: null,
tlrc: null,
rlrc: null,
lxlrc: null,
rawlrc: null,
})
} else resetPlayerMusicInfo()
}
/**
* 设置当前播放歌曲的信息
* @param listId 歌曲所属的列表id
* @param musicInfo 歌曲信息
* @param isTempPlay 是否临时播放
*/
export const setPlayMusicInfo = (listId: string | null, musicInfo: LX.Download.ListItem | LX.Music.MusicInfo | null, isTempPlay: boolean = false) => {
musicInfo = toRaw(musicInfo)
playMusicInfo.listId = listId
playMusicInfo.musicInfo = musicInfo
playMusicInfo.isTempPlay = isTempPlay
setPlayerMusicInfo(musicInfo)
setProgress(0, 0)
if (musicInfo == null) {
playInfo.playIndex = -1
playInfo.playerListId = null
playInfo.playerPlayIndex = -1
} else {
const { playIndex, playerPlayIndex } = getPlayIndex(listId, musicInfo, isTempPlay)
playInfo.playIndex = playIndex
playInfo.playerPlayIndex = playerPlayIndex
window.app_event.musicToggled()
}
}
/**
* 设置当前播放歌曲的音质(如果是在线歌曲)
* @param musicInfo 歌曲信息
*/
export const setPlayMusicInfoQuality = (musicInfo: LX.Music.MusicInfo | LX.Download.ListItem) => {
if (!('progress' in musicInfo)) {
playMusicInfo.quality = musicInfo.source !== 'local'
? getPlayQuality(appSetting['player.playQuality'], musicInfo)
: undefined
} else {
playMusicInfo.quality = undefined
}
}
/**
* 将歌曲添加到已播放列表
* @param playMusicInfo playMusicInfo对象
*/
export const addPlayedList = (playMusicInfo: LX.Player.PlayMusicInfo) => {
const id = playMusicInfo.musicInfo.id
if (playedList.some(m => m.musicInfo.id === id)) return
playedList.push(playMusicInfo)
}
/**
* 将歌曲从已播放列表移除
* @param index 歌曲位置
*/
export const removePlayedList = (index: number) => {
playedList.splice(index, 1)
}
/**
* 清空已播放列表
*/
export const clearPlayedList = () => {
playedList.splice(0, playedList.length)
}
/**
* 添加歌曲到稍后播放列表
* @param list 歌曲列表
*/
export const addTempPlayList = (list: LX.Player.TempPlayListItem[]) => {
const topList: Array<Omit<LX.Player.TempPlayListItem, 'top'>> = []
const bottomList = list.filter(({ isTop, ...musicInfo }) => {
if (isTop) {
topList.push(musicInfo)
return false
}
return true
})
if (topList.length) arrUnshift(tempPlayList, topList.map(({ musicInfo, listId }) => ({ musicInfo, listId, isTempPlay: true })))
if (bottomList.length) arrPush(tempPlayList, bottomList.map(({ musicInfo, listId }) => ({ musicInfo, listId, isTempPlay: true })))
if (!playMusicInfo.musicInfo) void playNext()
}
/**
* 从稍后播放列表移除歌曲
* @param index 歌曲位置
*/
export const removeTempPlayList = (index: number) => {
tempPlayList.splice(index, 1)
}
/**
* 清空稍后播放列表
*/
export const clearTempPlayeList = () => {
tempPlayList.splice(0, tempPlayList.length)
}