@@ -35,7 +35,7 @@ class Player {
3535 // 定时器
3636 private playerInterval : ReturnType < typeof setInterval > | undefined ;
3737 // 自动关闭定时器
38- private autoCloseTimer : ReturnType < typeof setTimeout > | undefined ;
38+ private autoCloseInterval : ReturnType < typeof setInterval > | undefined ;
3939 // 频谱数据
4040 private audioContext : AudioContext | null = null ;
4141 private analyser : AnalyserNode | null = null ;
@@ -54,8 +54,6 @@ class Player {
5454 this . player = new Howl ( { src : [ "" ] , format : allowPlayFormat , autoplay : false } ) ;
5555 // 初始化媒体会话
5656 this . initMediaSession ( ) ;
57- // 初始化自动关闭定时器
58- this . toggleAutoCloseTimer ( ) ;
5957 }
6058 /**
6159 * 获取当前播放歌曲
@@ -429,15 +427,19 @@ class Player {
429427 if ( currentSessionId !== this . playSessionId ) return ;
430428 // statusStore.playStatus = false;
431429 console . log ( "⏹️ song end:" , playSongData ) ;
432-
430+
433431 // 检查是否需要在歌曲结束时执行自动关闭
434432 const statusStore = useStatusStore ( ) ;
435- if ( statusStore . autoClose . enable && statusStore . autoClose . waitSongEnd && statusStore . autoClose . remainTime <= 0 ) {
433+ if (
434+ statusStore . autoClose . enable &&
435+ statusStore . autoClose . waitSongEnd &&
436+ statusStore . autoClose . remainTime <= 0
437+ ) {
436438 // 执行自动关闭
437439 this . executeAutoClose ( ) ;
438440 return ;
439441 }
440-
442+
441443 this . nextOrPrev ( "next" ) ;
442444 } ) ;
443445 // 错误
@@ -899,12 +901,6 @@ class Player {
899901 statusStore . lyricIndex = - 1 ;
900902 statusStore . currentTime = 0 ;
901903 statusStore . progress = 0 ;
902-
903- // 重置自动关闭计时器(切换歌曲时重新开始计时)
904- if ( statusStore . autoClose . enable ) {
905- this . startAutoCloseTimer ( statusStore . autoClose . time , statusStore . autoClose . waitSongEnd ) ;
906- }
907-
908904 // 暂停
909905 await this . pause ( false ) ;
910906 // 初始化播放器(不传入seek参数,确保从头开始播放)
@@ -1197,12 +1193,7 @@ class Player {
11971193 statusStore . lyricIndex = - 1 ;
11981194 statusStore . currentTime = 0 ;
11991195 statusStore . progress = 0 ;
1200-
1201- // 重置自动关闭计时器(切换歌曲时重新开始计时)
1202- if ( statusStore . autoClose . enable ) {
1203- this . startAutoCloseTimer ( statusStore . autoClose . time , statusStore . autoClose . waitSongEnd ) ;
1204- }
1205-
1196+
12061197 // 清理并播放(不传入seek参数,确保从头开始播放)
12071198 await this . initPlayer ( true , 0 ) ;
12081199 }
@@ -1446,72 +1437,47 @@ class Player {
14461437 }
14471438 /**
14481439 * 开始定时关闭
1449- * @param time 关闭时间(秒 )
1450- * @param waitSongEnd 是否等待歌曲结束
1440+ * @param time 关闭时间(分钟 )
1441+ * @param remainTime 剩余时间(秒)
14511442 */
1452- startAutoCloseTimer ( time : number , waitSongEnd : boolean = true ) {
1443+ startAutoCloseTimer ( time : number , remainTime : number ) {
14531444 const statusStore = useStatusStore ( ) ;
1454- // 清除之前的定时器
1455- clearTimeout ( this . autoCloseTimer ) ;
1445+ if ( ! time || ! remainTime ) return ;
1446+ // 如已有定时器在运行,先停止以防叠加
1447+ if ( this . autoCloseInterval ) {
1448+ clearInterval ( this . autoCloseInterval ) ;
1449+ this . autoCloseInterval = undefined ;
1450+ }
14561451 // 重置剩余时间
1457- statusStore . autoClose = {
1452+ Object . assign ( statusStore . autoClose , {
14581453 enable : true ,
14591454 time,
1460- remainTime : time ,
1461- waitSongEnd,
1462- } ;
1455+ remainTime,
1456+ } ) ;
14631457 // 开始减少剩余时间
1464- const { pause } = useIntervalFn ( ( ) => {
1465- if ( statusStore . autoClose . remainTime <= 0 ) pause ( ) ;
1458+ this . autoCloseInterval = setInterval ( ( ) => {
1459+ if ( statusStore . autoClose . remainTime <= 0 ) {
1460+ clearInterval ( this . autoCloseInterval ) ;
1461+ this . autoCloseInterval = undefined ;
1462+ if ( ! statusStore . autoClose . waitSongEnd ) {
1463+ this . executeAutoClose ( ) ;
1464+ }
1465+ return ;
1466+ }
14661467 statusStore . autoClose . remainTime -- ;
14671468 } , 1000 ) ;
1468- // 设置新的定时器
1469- this . autoCloseTimer = setTimeout ( ( ) => {
1470- pause ( ) ;
1471- statusStore . autoClose . remainTime = 0 ;
1472- // 根据设置决定如何关闭
1473- if ( statusStore . autoClose . waitSongEnd ) {
1474- // 等待歌曲结束,不在这里执行关闭,而是在歌曲结束事件中处理
1475- console . log ( "⏰ 自动关闭计时结束,等待当前歌曲播放完毕" ) ;
1476- } else {
1477- // 立即执行关闭
1478- this . executeAutoClose ( ) ;
1479- }
1480- } , time * 1000 ) ;
14811469 }
1482- /**
1483- * 开启或者停止自动关闭定时器
1484- */
1485- toggleAutoCloseTimer ( ) {
1486- const statusStore = useStatusStore ( ) ;
1487- if ( statusStore . autoClose . enable ) {
1488- this . startAutoCloseTimer ( statusStore . autoClose . time , statusStore . autoClose . waitSongEnd ) ;
1489- } else {
1490- clearTimeout ( this . autoCloseTimer ) ;
1491- }
1492- }
1493-
14941470 /**
14951471 * 执行自动关闭操作
14961472 */
14971473 private executeAutoClose ( ) {
14981474 console . log ( "🔄 执行自动关闭" ) ;
14991475 // 暂停播放
15001476 this . pause ( ) ;
1501- // 清除定时器
1502- clearTimeout ( this . autoCloseTimer ) ;
15031477 // 重置状态
1504- const statusStore = useStatusStore ( ) ;
1505- statusStore . autoClose . enable = false ;
1506- statusStore . autoClose . remainTime = 0 ;
1507-
1508- // 如果是 Electron 环境,关闭应用
1509- if ( isElectron ) {
1510- window . electron . ipcRenderer . send ( "app-close" ) ;
1511- } else {
1512- // 浏览器环境,显示提示信息
1513- window . $message . info ( "自动关闭已触发,播放已暂停" ) ;
1514- }
1478+ const { autoClose } = useStatusStore ( ) ;
1479+ autoClose . enable = false ;
1480+ autoClose . remainTime = autoClose . time * 60 ;
15151481 }
15161482}
15171483
0 commit comments