From 22ccb5b5384c570d34a4bace350ded5fb511b756 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A7=89?= Date: Wed, 4 Oct 2023 06:32:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E5=8D=95=E7=8B=AC=E7=9A=84?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E6=A0=87=E8=AF=86=E9=9F=B3=E4=B9=90=E6=98=AF?= =?UTF-8?q?=E5=90=A6=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + public/index.html | 8 +++++--- public/music.js | 6 +++--- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index f8c2b21..2caf7a5 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ webrtc 实现的 p2p 信道 - [x] 稳定通信 - [x] 分片请求时立即播放 - [ ] 上锁防止连续重复加载同一个造成分片混乱 + - [x] 使用单独的状态标识音乐是否缓存 - [ ] 集群分发 - [ ] 下载加速 - [ ] 即时通讯 diff --git a/public/index.html b/public/index.html index 38eb514..22e9907 100644 --- a/public/index.html +++ b/public/index.html @@ -29,10 +29,12 @@ return tmp.buffer } - // 读取本地音乐列表(本地缓存) + // 读取本地音乐列表并标识为缓存状态(本地缓存) const database = new IndexedDB('musicDatabase', 1, 'musicObjectStore') await database.open() - const list = await database.getAll() + const list = (await database.getAll()).map(item => { + return { save: true, ...item } + }) // 读取本地用户名(本地缓存) const name = localStorage.getItem('username') ?? '游客' @@ -70,7 +72,7 @@ onlike: (item, list) => { console.log('喜欢音乐', item.name) if (item.arrayBuffer) { - database.add(item) + database.add({ save:true, ...item }) clientList.send('base', JSON.stringify({ type: 'set_music_list', list: list.map(({ id, name, size, type }) => ({ id, name, size, type })) diff --git a/public/music.js b/public/music.js index efe665f..68d9e19 100644 --- a/public/music.js +++ b/public/music.js @@ -110,10 +110,10 @@ export default class MusicList { } }), Button({ - textContent: item.arrayBuffer ? '移除' : '缓存', + textContent: item.save ? '移除' : '缓存', onclick: event => { event.stopPropagation() - if (item.arrayBuffer) { + if (item.save) { event.target.textContent = '缓存' this.unlike(item) } else { @@ -137,7 +137,7 @@ export default class MusicList { await this.event.onload(item) } async play(item) { - if (!item.arrayBuffer) { + if (!item.save) { // 边加载边播放 const mediaSource = new MediaSource() this.audio.src = URL.createObjectURL(mediaSource)