Skip to content

Commit

Permalink
🐞 fix: 解决音频资源过期问题
Browse files Browse the repository at this point in the history
  • Loading branch information
imsyy committed Jan 3, 2024
1 parent 1a63771 commit 0a7761f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/utils/Player.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Howl, Howler } from "howler";
import { musicData, siteStatus, siteSettings } from "@/stores";
import { getSongUrl, getSongLyric, songScrobble } from "@/api/song";
import { checkPlatform, getLocalCoverData } from "@/utils/helper";
import { checkPlatform, getLocalCoverData, getBlobUrlFromUrl } from "@/utils/helper";
import { decode as base642Buffer } from "@/utils/base64";
import { getSongPlayTime } from "@/utils/timeTools";
import { getCoverGradient } from "@/utils/cover-color";
Expand Down Expand Up @@ -207,7 +207,6 @@ const getFromUnblockMusic = async (data, status, playNow) => {
* @param {number} seek - 初始播放进度( 默认为 0 )
*/
export const createPlayer = async (src, autoPlay = true) => {
console.log("播放地址:", src);
try {
// pinia
const music = musicData();
Expand All @@ -216,9 +215,12 @@ export const createPlayer = async (src, autoPlay = true) => {
const { playSongSource, playList } = music;
// 当前播放歌曲数据
const playSongData = music.getPlaySongData;
// 获取播放链接
const blobUrl = await getBlobUrlFromUrl(src);
console.log("播放地址:", blobUrl);
// 初始化播放器
player = new Howl({
src: [src],
src: [blobUrl],
format: ["mp3", "flac", "dolby", "webm"],
html5: true,
preload: true,
Expand Down Expand Up @@ -260,8 +262,8 @@ export const createPlayer = async (src, autoPlay = true) => {
status.playMode === "dj"
? "电台节目"
: Array.isArray(playSongData.artists)
? playSongData.artists.map((ar) => ar.name).join(" / ")
: playSongData.artists || "未知歌手";
? playSongData.artists.map((ar) => ar.name).join(" / ")
: playSongData.artists || "未知歌手";
electron.ipcRenderer.send("songNameChange", songName + " - " + songArtist);
}
// 听歌打卡
Expand Down Expand Up @@ -649,8 +651,8 @@ const initMediaSession = async (data, cover, islocal, isDj) => {
artist: isDj
? "电台节目"
: islocal
? data.artists
: data.artists?.map((a) => a.name)?.join(" & "),
? data.artists
: data.artists?.map((a) => a.name)?.join(" & "),
album: isDj ? "电台节目" : islocal ? data.album : data.album.name,
artwork: islocal
? [
Expand Down
32 changes: 32 additions & 0 deletions src/utils/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,35 @@ export const formatBytes = (bytes, decimals = 2) => {
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + " " + sizes[i];
};

/**
* 获取音频文件的 Blob 链接
* @param {string} url - 音频文件的网络链接
* @returns {Promise<string>} - 包含 Blob 链接的 Promise
* @throws {Error} 如果获取 Blob 链接时出现错误
*/
// 上次生成的 BlobUrl
let lastBlobUrl = null;
export const getBlobUrlFromUrl = async (url) => {
try {
// 是否为网络链接
if (!url.startsWith("https://") && !url.startsWith("https://")) {
return url;
}
// 获取音频文件数据
const response = await fetch(url);
// 检查请求是否成功
if (!response.ok) {
throw new Error("获取音频资源失败:", response.statusText);
}
const blob = await response.blob();
// 清理过期的 Blob 链接
if (lastBlobUrl) URL.revokeObjectURL(lastBlobUrl);
// 转换为本地 Blob 链接
lastBlobUrl = URL.createObjectURL(blob);
return lastBlobUrl;
} catch (error) {
console.error("获取 Blob 链接遇到错误:" + error);
throw error;
}
};

0 comments on commit 0a7761f

Please sign in to comment.