@@ -45,6 +45,7 @@ class AudioService {
45
45
private operationLock = false ;
46
46
private operationLockTimer : NodeJS . Timeout | null = null ;
47
47
private operationLockTimeout = 5000 ; // 5秒超时
48
+ private operationLockStartTime : number = 0 ;
48
49
49
50
constructor ( ) {
50
51
if ( 'mediaSession' in navigator ) {
@@ -363,11 +364,19 @@ class AudioService {
363
364
364
365
// 设置操作锁,带超时自动释放
365
366
private setOperationLock ( ) : boolean {
367
+ // 如果锁已经存在,检查是否超时
366
368
if ( this . operationLock ) {
369
+ const currentTime = Date . now ( ) ;
370
+ if ( currentTime - this . operationLockStartTime > this . operationLockTimeout ) {
371
+ console . warn ( '操作锁已超时,强制释放' ) ;
372
+ this . releaseOperationLock ( ) ;
373
+ return true ;
374
+ }
367
375
return false ;
368
376
}
369
377
370
378
this . operationLock = true ;
379
+ this . operationLockStartTime = Date . now ( ) ;
371
380
372
381
// 清除之前的定时器
373
382
if ( this . operationLockTimer ) {
@@ -377,8 +386,7 @@ class AudioService {
377
386
// 设置超时自动释放锁
378
387
this . operationLockTimer = setTimeout ( ( ) => {
379
388
console . warn ( '操作锁超时自动释放' ) ;
380
- this . operationLock = false ;
381
- this . operationLockTimer = null ;
389
+ this . releaseOperationLock ( ) ;
382
390
} , this . operationLockTimeout ) ;
383
391
384
392
return true ;
@@ -387,6 +395,7 @@ class AudioService {
387
395
// 释放操作锁
388
396
private releaseOperationLock ( ) : void {
389
397
this . operationLock = false ;
398
+ this . operationLockStartTime = 0 ;
390
399
391
400
if ( this . operationLockTimer ) {
392
401
clearTimeout ( this . operationLockTimer ) ;
@@ -485,6 +494,7 @@ class AudioService {
485
494
} else {
486
495
// 发送URL过期事件,通知外部需要重新获取URL
487
496
this . emit ( 'url_expired' , this . currentTrack ) ;
497
+ this . releaseOperationLock ( ) ;
488
498
reject ( new Error ( '音频加载失败,请尝试切换其他歌曲' ) ) ;
489
499
}
490
500
} ,
@@ -497,6 +507,7 @@ class AudioService {
497
507
} else {
498
508
// 发送URL过期事件,通知外部需要重新获取URL
499
509
this . emit ( 'url_expired' , this . currentTrack ) ;
510
+ this . releaseOperationLock ( ) ;
500
511
reject ( new Error ( '音频播放失败,请尝试切换其他歌曲' ) ) ;
501
512
}
502
513
} ,
0 commit comments