chore: 可以上传播放版本

This commit is contained in:
2025-12-15 18:26:49 +08:00
parent 5f6d43e66b
commit 2d417cd631
4 changed files with 76 additions and 6 deletions

View File

@@ -359,7 +359,12 @@ export const getMusicConfig = async () => {
*/ */
export const saveMusicConfig = async (musicConfig) => { export const saveMusicConfig = async (musicConfig) => {
const config = await readConfig(); const config = await readConfig();
config.music = musicConfig; // 确保音量是数字类型
const normalizedMusicConfig = {
...musicConfig,
volume: typeof musicConfig.volume === 'string' ? parseFloat(musicConfig.volume) : musicConfig.volume
};
config.music = normalizedMusicConfig;
return await writeConfig(config); return await writeConfig(config);
}; };

View File

@@ -3,7 +3,7 @@ class MusicPlayer {
constructor() { constructor() {
this.audio = null; this.audio = null;
this.isPlaying = false; this.isPlaying = false;
this.defaultPath = "/assets/music/background.mp3"; this.defaultPath = "";
this.enabled = false; this.enabled = false;
this.volume = 0.5; // 默认音量50% this.volume = 0.5; // 默认音量50%
} }
@@ -23,36 +23,55 @@ class MusicPlayer {
* @param {boolean} enabled 播放开关 * @param {boolean} enabled 播放开关
*/ */
initMusicConfig(filePath, enabled, volume = 0.5) { initMusicConfig(filePath, enabled, volume = 0.5) {
console.log("初始化音乐配置:", { filePath, enabled, volume });
this.enabled = enabled; this.enabled = enabled;
this.volume = volume; // 确保音量是数字类型
this.volume = typeof volume === 'string' ? parseFloat(volume) : volume;
console.log("处理后的音量值:", this.volume, "类型:", typeof this.volume);
let validPath = this.defaultPath; let validPath = this.defaultPath;
if (filePath && filePath.endsWith('.mp3')) { if (filePath && filePath.endsWith('.mp3')) {
validPath = filePath; validPath = filePath;
} else if (filePath) { } else if (filePath) {
console.warn(`音乐路径无效非MP3格式${filePath},使用兜底路径`); console.warn(`音乐路径无效非MP3格式${filePath},使用兜底路径`);
} }
console.log("使用的音乐路径:", validPath);
if (this.audio) { if (this.audio) {
this.audio.pause(); this.audio.pause();
this.audio = null; this.audio = null;
} }
this.audio = new Audio(validPath); this.audio = new Audio(validPath);
this.audio.loop = true; this.audio.loop = true;
this.audio.volume = this.volume; this.audio.volume = this.volume;
console.log("音频对象创建完成,音量设置为:", this.audio.volume);
} }
/** /**
* 播放音乐(保留原有逻辑,适配开关) * 播放音乐(保留原有逻辑,适配开关)
*/ */
play() { play() {
console.log("调用 play 方法,当前状态:", { enabled: this.enabled, hasAudio: !!this.audio, isPlaying: this.isPlaying });
if (!this.enabled) { if (!this.enabled) {
console.log("首页播放开关未开启,跳过音乐播放"); console.log("首页播放开关未开启,跳过音乐播放");
return; return;
} }
if (!this.audio) { if (!this.audio) {
console.warn("音频对象未初始化");
this.initMusicConfig(this.defaultPath, false); this.initMusicConfig(this.defaultPath, false);
console.warn("未初始化音乐配置,使用兜底路径且关闭播放开关"); console.warn("未初始化音乐配置,使用兜底路径且关闭播放开关");
return; return;
} }
console.log("音频源路径:", this.audio.src);
console.log("音频就绪状态:", this.audio.readyState);
if (!this.isPlaying) { if (!this.isPlaying) {
this.audio.play() this.audio.play()
.then(() => { .then(() => {
@@ -61,8 +80,15 @@ class MusicPlayer {
}) })
.catch(err => { .catch(err => {
console.error("音乐播放失败(浏览器自动播放限制/路径错误):", err); console.error("音乐播放失败(浏览器自动播放限制/路径错误):", err);
console.error("错误详情:", {
name: err.name,
message: err.message,
code: err.code
});
this.isPlaying = false; this.isPlaying = false;
}); });
} else {
console.log("音乐已在播放中");
} }
} }
@@ -93,8 +119,10 @@ setMuted(muted) {
*/ */
setVolume(volume) { setVolume(volume) {
if (this.audio) { if (this.audio) {
// 确保音量是数字类型
const numericVolume = typeof volume === 'string' ? parseFloat(volume) : volume;
// 限制音量范围在0.0到1.0之间 // 限制音量范围在0.0到1.0之间
this.volume = Math.max(0, Math.min(1, volume)); this.volume = Math.max(0, Math.min(1, numericVolume));
this.audio.volume = this.volume; this.audio.volume = this.volume;
console.log(`音乐音量已设置为: ${Math.round(this.volume * 100)}%`); console.log(`音乐音量已设置为: ${Math.round(this.volume * 100)}%`);
} }

View File

@@ -1488,7 +1488,7 @@ const saveData = async () => {
currentConfig.music = { currentConfig.music = {
enabled: musicEnabled.value, enabled: musicEnabled.value,
filePath: currentMusicPath.value, filePath: currentMusicPath.value,
volume: musicVolume.value volume: parseFloat(musicVolume.value) // 确保音量是数字类型
}; };
// 一次性保存所有配置 // 一次性保存所有配置

View File

@@ -406,13 +406,18 @@ const taskSettings = ref({
const localMusicConfig = ref({ const localMusicConfig = ref({
enabled: true, enabled: true,
filePath: '' filePath: '',
volume: 1
}); });
// 添加首次交互处理函数 // 添加首次交互处理函数
const handleFirstInteraction = () => { const handleFirstInteraction = () => {
console.log("用户首次交互触发");
if (localMusicConfig.value.enabled) { if (localMusicConfig.value.enabled) {
console.log("尝试播放音乐...");
musicPlayer.play(); musicPlayer.play();
} else {
console.log("音乐未启用,跳过播放");
} }
}; };
@@ -447,6 +452,11 @@ onBeforeMount(async () => {
if (config.music) { if (config.music) {
localMusicConfig.value = config.music; localMusicConfig.value = config.music;
console.log("从服务器加载的音乐配置:", config.music);
console.log("音乐文件路径:", config.music.filePath);
console.log("音乐是否启用:", config.music.enabled);
console.log("音乐音量:", config.music.volume);
console.log("音量类型:", typeof config.music.volume);
} }
@@ -895,15 +905,42 @@ onMounted(async () => {
try { try {
// 异步初始化数据 // 异步初始化数据
await initializeData(); await initializeData();
localMusicConfig.value = await readConfig().music;
if (localMusicConfig.value.enabled) { if (localMusicConfig.value.enabled) {
// 获取音量设置如果没有则使用默认值0.5 // 获取音量设置如果没有则使用默认值0.5
const volume = localMusicConfig.value.volume !== undefined ? localMusicConfig.value.volume : 0.5; const volume = localMusicConfig.value.volume !== undefined ? localMusicConfig.value.volume : 0.5;
console.log("音乐配置信息:", localMusicConfig.value);
console.log("音乐文件路径:", localMusicConfig.value.filePath);
console.log("音乐是否启用:", localMusicConfig.value.enabled);
console.log("音乐音量:", volume);
console.log("音量类型:", typeof volume);
// 检查音乐文件是否存在
if (localMusicConfig.value.filePath) {
console.log("正在检查音乐文件是否存在...");
fetch(localMusicConfig.value.filePath)
.then(response => {
console.log("音乐文件状态:", response.status, response.statusText);
if (response.ok) {
console.log("音乐文件存在,可以正常访问");
} else {
console.error("音乐文件无法访问,状态码:", response.status);
}
})
.catch(error => {
console.error("检查音乐文件时出错:", error);
});
}
musicPlayer.initMusicConfig(localMusicConfig.value.filePath, localMusicConfig.value.enabled, volume); musicPlayer.initMusicConfig(localMusicConfig.value.filePath, localMusicConfig.value.enabled, volume);
// 注意由于浏览器自动播放策略限制这里不直接调用play() // 注意由于浏览器自动播放策略限制这里不直接调用play()
// 而是等待用户的第一次交互(点击)后再播放 // 而是等待用户的第一次交互(点击)后再播放
console.log("音乐已准备就绪,等待用户首次交互后播放..."); console.log("音乐已准备就绪,等待用户首次交互后播放...");
} else { } else {
musicPlayer.pause(); musicPlayer.pause();
console.log("音乐未启用,已暂停");
} }
// 更新本地显示配置确保columnAlignments属性存在 // 更新本地显示配置确保columnAlignments属性存在
if (displayConfig) { if (displayConfig) {