From cb58abbbfda4b48cf998d7b598cd89a778c97577 Mon Sep 17 00:00:00 2001 From: alger Date: Wed, 7 May 2025 22:36:55 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20chore:=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=E9=94=81=E9=80=BB=E8=BE=91=EF=BC=8C=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E8=B6=85=E6=97=B6=E6=A3=80=E6=9F=A5=E6=9C=BA=E5=88=B6?= =?UTF-8?q?=EF=BC=8C=E7=A1=AE=E4=BF=9D=E6=93=8D=E4=BD=9C=E9=94=81=E5=9C=A8?= =?UTF-8?q?=E8=B6=85=E6=97=B6=E5=90=8E=E8=87=AA=E5=8A=A8=E9=87=8A=E6=94=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/renderer/services/audioService.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/renderer/services/audioService.ts b/src/renderer/services/audioService.ts index 53a6a8c..25729a7 100644 --- a/src/renderer/services/audioService.ts +++ b/src/renderer/services/audioService.ts @@ -45,6 +45,7 @@ class AudioService { private operationLock = false; private operationLockTimer: NodeJS.Timeout | null = null; private operationLockTimeout = 5000; // 5秒超时 + private operationLockStartTime: number = 0; constructor() { if ('mediaSession' in navigator) { @@ -363,11 +364,19 @@ class AudioService { // 设置操作锁,带超时自动释放 private setOperationLock(): boolean { + // 如果锁已经存在,检查是否超时 if (this.operationLock) { + const currentTime = Date.now(); + if (currentTime - this.operationLockStartTime > this.operationLockTimeout) { + console.warn('操作锁已超时,强制释放'); + this.releaseOperationLock(); + return true; + } return false; } this.operationLock = true; + this.operationLockStartTime = Date.now(); // 清除之前的定时器 if (this.operationLockTimer) { @@ -377,8 +386,7 @@ class AudioService { // 设置超时自动释放锁 this.operationLockTimer = setTimeout(() => { console.warn('操作锁超时自动释放'); - this.operationLock = false; - this.operationLockTimer = null; + this.releaseOperationLock(); }, this.operationLockTimeout); return true; @@ -387,6 +395,7 @@ class AudioService { // 释放操作锁 private releaseOperationLock(): void { this.operationLock = false; + this.operationLockStartTime = 0; if (this.operationLockTimer) { clearTimeout(this.operationLockTimer); @@ -485,6 +494,7 @@ class AudioService { } else { // 发送URL过期事件,通知外部需要重新获取URL this.emit('url_expired', this.currentTrack); + this.releaseOperationLock(); reject(new Error('音频加载失败,请尝试切换其他歌曲')); } }, @@ -497,6 +507,7 @@ class AudioService { } else { // 发送URL过期事件,通知外部需要重新获取URL this.emit('url_expired', this.currentTrack); + this.releaseOperationLock(); reject(new Error('音频播放失败,请尝试切换其他歌曲')); } },