使用单独的状态标识音乐是否缓存

This commit is contained in:
2023-10-04 06:32:53 +08:00
parent a86f0c77d8
commit 22ccb5b538
3 changed files with 9 additions and 6 deletions

View File

@ -12,6 +12,7 @@ webrtc 实现的 p2p 信道
- [x] 稳定通信 - [x] 稳定通信
- [x] 分片请求时立即播放 - [x] 分片请求时立即播放
- [ ] 上锁防止连续重复加载同一个造成分片混乱 - [ ] 上锁防止连续重复加载同一个造成分片混乱
- [x] 使用单独的状态标识音乐是否缓存
- [ ] 集群分发 - [ ] 集群分发
- [ ] 下载加速 - [ ] 下载加速
- [ ] 即时通讯 - [ ] 即时通讯

View File

@ -29,10 +29,12 @@
return tmp.buffer return tmp.buffer
} }
// 读取本地音乐列表(本地缓存) // 读取本地音乐列表并标识为缓存状态(本地缓存)
const database = new IndexedDB('musicDatabase', 1, 'musicObjectStore') const database = new IndexedDB('musicDatabase', 1, 'musicObjectStore')
await database.open() await database.open()
const list = await database.getAll() const list = (await database.getAll()).map(item => {
return { save: true, ...item }
})
// 读取本地用户名(本地缓存) // 读取本地用户名(本地缓存)
const name = localStorage.getItem('username') ?? '游客' const name = localStorage.getItem('username') ?? '游客'
@ -70,7 +72,7 @@
onlike: (item, list) => { onlike: (item, list) => {
console.log('喜欢音乐', item.name) console.log('喜欢音乐', item.name)
if (item.arrayBuffer) { if (item.arrayBuffer) {
database.add(item) database.add({ save:true, ...item })
clientList.send('base', JSON.stringify({ clientList.send('base', JSON.stringify({
type: 'set_music_list', type: 'set_music_list',
list: list.map(({ id, name, size, type }) => ({ id, name, size, type })) list: list.map(({ id, name, size, type }) => ({ id, name, size, type }))

View File

@ -110,10 +110,10 @@ export default class MusicList {
} }
}), }),
Button({ Button({
textContent: item.arrayBuffer ? '移除' : '缓存', textContent: item.save ? '移除' : '缓存',
onclick: event => { onclick: event => {
event.stopPropagation() event.stopPropagation()
if (item.arrayBuffer) { if (item.save) {
event.target.textContent = '缓存' event.target.textContent = '缓存'
this.unlike(item) this.unlike(item)
} else { } else {
@ -137,7 +137,7 @@ export default class MusicList {
await this.event.onload(item) await this.event.onload(item)
} }
async play(item) { async play(item) {
if (!item.arrayBuffer) { if (!item.save) {
// 边加载边播放 // 边加载边播放
const mediaSource = new MediaSource() const mediaSource = new MediaSource()
this.audio.src = URL.createObjectURL(mediaSource) this.audio.src = URL.createObjectURL(mediaSource)