mirror of
https://github.com/qier222/YesPlayMusic.git
synced 2025-02-01 14:31:19 +08:00
feat: support cache songs (#80)
fix: cache unblock song refactor: play first and then cache it
This commit is contained in:
parent
993f867109
commit
4128d9ac3b
|
@ -1,5 +1,5 @@
|
||||||
import { updateMediaSessionMetaData } from "@/utils/mediaSession";
|
import { updateMediaSessionMetaData } from "@/utils/mediaSession";
|
||||||
import { getTrackDetail, scrobble, getMP3 } from "@/api/track";
|
import { getTrackDetail, scrobble, getMP3 as getMP3Api } from "@/api/track";
|
||||||
import { isAccountLoggedIn } from "@/utils/auth";
|
import { isAccountLoggedIn } from "@/utils/auth";
|
||||||
import { updateHttps } from "@/utils/common";
|
import { updateHttps } from "@/utils/common";
|
||||||
import localforage from "localforage";
|
import localforage from "localforage";
|
||||||
|
@ -40,18 +40,19 @@ export default {
|
||||||
updateMediaSessionMetaData(track);
|
updateMediaSessionMetaData(track);
|
||||||
document.title = `${track.name} · ${track.ar[0].name} - YesPlayMusic`;
|
document.title = `${track.name} · ${track.ar[0].name} - YesPlayMusic`;
|
||||||
|
|
||||||
|
let unblockSongUrl = null;
|
||||||
if (track.playable === false) {
|
if (track.playable === false) {
|
||||||
let res = undefined;
|
let res = undefined;
|
||||||
if (process.env.IS_ELECTRON === true) {
|
if (process.env.IS_ELECTRON === true) {
|
||||||
res = ipcRenderer.sendSync("unblock-music", track);
|
res = ipcRenderer.sendSync("unblock-music", track);
|
||||||
}
|
}
|
||||||
if (res?.url) {
|
if (res?.url) {
|
||||||
commitMP3(res.url);
|
unblockSongUrl = res.url;
|
||||||
} else {
|
} else {
|
||||||
dispatch("nextTrack");
|
dispatch("nextTrack");
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function commitMP3(mp3) {
|
function commitMP3(mp3) {
|
||||||
commit("replaceMP3", { mp3, autoplay });
|
commit("replaceMP3", { mp3, autoplay });
|
||||||
|
@ -59,31 +60,56 @@ export default {
|
||||||
dispatch("nextTrack");
|
dispatch("nextTrack");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getMP3(id) {
|
||||||
|
return getMP3Api(id).then((data) => {
|
||||||
|
// 未知情况下会没有返回数据导致报错,增加防范逻辑
|
||||||
|
if (data.data[0]) {
|
||||||
|
const url = updateHttps(data.data[0].url);
|
||||||
|
commitMP3(url);
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (isAccountLoggedIn()) {
|
if (isAccountLoggedIn()) {
|
||||||
if (store.state.settings.automaticallyCacheSongs === true) {
|
if (store.state.settings.automaticallyCacheSongs === true) {
|
||||||
let tracks = localforage.createInstance({
|
let tracks = localforage.createInstance({
|
||||||
name: "tracks",
|
name: "tracks",
|
||||||
});
|
});
|
||||||
tracks.getItem(`${track.id}`).then((t) => {
|
tracks
|
||||||
|
.getItem(`${track.id}`)
|
||||||
|
.then((t) => {
|
||||||
if (t !== null) {
|
if (t !== null) {
|
||||||
commitMP3(URL.createObjectURL(t.mp3));
|
const blob = new Blob([t.mp3]);
|
||||||
|
commitMP3(URL.createObjectURL(blob));
|
||||||
} else {
|
} else {
|
||||||
cacheTrack(`${track.id}`).then((t) => {
|
if (unblockSongUrl) {
|
||||||
commitMP3(URL.createObjectURL(t.mp3));
|
commitMP3(unblockSongUrl);
|
||||||
|
cacheTrack(`${track.id}`, unblockSongUrl);
|
||||||
|
} else {
|
||||||
|
getMP3(track.id).then((url) => {
|
||||||
|
cacheTrack(`${track.id}`, url);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
console.log(err.messaeg);
|
||||||
|
if (unblockSongUrl) {
|
||||||
|
commitMP3(unblockSongUrl);
|
||||||
|
} else {
|
||||||
|
getMP3(track.id);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
getMP3(track.id).then((data) => {
|
getMP3(track.id);
|
||||||
// 未知情况下会没有返回数据导致报错,增加防范逻辑
|
|
||||||
if (data.data[0]) {
|
|
||||||
const url = updateHttps(data.data[0].url);
|
|
||||||
commitMP3(url);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
commitMP3(`https://music.163.com/song/media/outer/url?id=${track.id}`);
|
commitMP3(
|
||||||
|
unblockSongUrl ||
|
||||||
|
`https://music.163.com/song/media/outer/url?id=${track.id}`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,11 +1,10 @@
|
||||||
// import axios from "axios";
|
import axios from "axios";
|
||||||
import localforage from "localforage";
|
import localforage from "localforage";
|
||||||
import { getMP3 } from "@/api/track";
|
|
||||||
|
|
||||||
export function cacheTrack(id) {
|
export function cacheTrack(id, url) {
|
||||||
// let tracks = localforage.createInstance({
|
let tracks = localforage.createInstance({
|
||||||
// name: "tracks",
|
name: "tracks",
|
||||||
// });
|
});
|
||||||
|
|
||||||
// TODO: limit cache songs number
|
// TODO: limit cache songs number
|
||||||
// tracks.length().then(function (length) {
|
// tracks.length().then(function (length) {
|
||||||
|
@ -17,16 +16,13 @@ export function cacheTrack(id) {
|
||||||
// });
|
// });
|
||||||
|
|
||||||
// TODO: cache track details
|
// TODO: cache track details
|
||||||
return getMP3(id).then((data) => {
|
return axios
|
||||||
// return axios
|
.get(url, {
|
||||||
// .get(data.data[0].url.replace(/^http:/, "https:"), {
|
responseType: "arraybuffer",
|
||||||
// responseType: "blob",
|
})
|
||||||
// })
|
.then((response) => {
|
||||||
// .then((data) => {
|
tracks.setItem(`${id}`, { mp3: response.data });
|
||||||
// tracks.setItem(`${id}`, { mp3: data.data });
|
return { mp3: response.data };
|
||||||
// return { mp3: data.data };
|
|
||||||
// });
|
|
||||||
return { mp3: data.data[0].url.replace(/^http:/, "https:") };
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,11 +33,11 @@ export function countDBSize(dbName) {
|
||||||
let trackSizes = [];
|
let trackSizes = [];
|
||||||
return db
|
return db
|
||||||
.iterate((value) => {
|
.iterate((value) => {
|
||||||
trackSizes.push(value.mp3.size);
|
trackSizes.push(value.mp3.byteLength);
|
||||||
})
|
})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
return {
|
return {
|
||||||
bytes: trackSizes.reduce((s1, s2) => s1 + s2),
|
bytes: trackSizes.reduce((s1, s2) => s1 + s2, 0),
|
||||||
length: trackSizes.length,
|
length: trackSizes.length,
|
||||||
};
|
};
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue
Block a user