Skip to content

Commit

Permalink
🐞 fix: 修复音乐缓存导致播放异常
Browse files Browse the repository at this point in the history
  • Loading branch information
imsyy committed Jan 5, 2024
1 parent 41eadb5 commit af39318
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/stores/siteSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const useSiteSettingsStore = defineStore("siteSettings", {
playSearch: false, // 是否播放全部搜索结果
showPlaylistCount: true, // 是否显示播放列表数量
showSpectrums: false, // 是否显示音乐频谱
useMusicCache: true, // 是否采用音乐缓存
useMusicCache: false, // 是否采用音乐缓存
// 数量部分
loadSize: 50, // 每页加载数量
// 歌词部分
Expand Down
8 changes: 1 addition & 7 deletions src/utils/Player.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ export const createPlayer = async (src, autoPlay = true) => {
const blobUrl = useMusicCache ? await getBlobUrlFromUrl(src) : src;
console.log("播放地址:", blobUrl);
// 初始化播放器
if (player) soundStop();
player = new Howl({
src: [blobUrl],
format: ["mp3", "flac", "dolby", "webm"],
Expand Down Expand Up @@ -503,13 +504,6 @@ export const setVolume = (volume) => {
player?.volume(Number(volume));
};

/**
* 检查是否存在于播放器且正在播放
*/
export const checkPlayer = () => {
return player && player?.playing();
};

/**
* 停止播放器
*/
Expand Down
4 changes: 2 additions & 2 deletions src/utils/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,8 @@ export const formatBytes = (bytes, decimals = 2) => {
let lastBlobUrl = null;
export const getBlobUrlFromUrl = async (url) => {
try {
// 清理过期的 Blob 链接
if (lastBlobUrl) URL.revokeObjectURL(lastBlobUrl);
// 是否为网络链接
if (!url.startsWith("https://") && !url.startsWith("https://") && !url.startsWith("blob:")) {
return url;
Expand All @@ -374,8 +376,6 @@ export const getBlobUrlFromUrl = async (url) => {
throw new Error("获取音频资源失败:", response.statusText);
}
const blob = await response.blob();
// 清理过期的 Blob 链接
if (lastBlobUrl) URL.revokeObjectURL(lastBlobUrl);
// 转换为本地 Blob 链接
lastBlobUrl = URL.createObjectURL(blob);
return lastBlobUrl;
Expand Down

0 comments on commit af39318

Please sign in to comment.