fix(metronome): 切后台/小窗后节拍声继续(放兜里也能听)

两层原因都修:

1. 微信限制(声明后台音频权限):
   InnerAudioContext 默认在小程序进后台时被强制暂停。
   必须显式声明 requiredBackgroundModes,iOS 才允许后台继续发声。
   - manifest.json mp-weixin 全局加 requiredBackgroundModes:["audio"]
   - pages.json 节拍器页 style 也加,双保险
   说明:Android 大部分情况无需此声明也能后台,iOS 必需。

2. 代码层(onHide 不再主动 stop):
   原 onHide 里 if(isPlaying) stop() 会在切后台/小窗时
   立刻把节拍器自己关掉,跟需求相反。

   新策略:
   - onHide 留空 → 切后台/小窗时节拍器继续工作
   - onUnmounted 才真正 stop + 释放屏幕常亮 → 用户主动离开
     节拍器页(navigateBack/重启)时清理资源

实际场景:健走时把手机塞口袋,锁屏或切到微信聊天,
节拍声依然继续响,无需把手机一直拿着看。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-05-26 09:19:19 +08:00
co-authored by Cursor
parent 3f999b312d
commit c1dfb01f53
3 changed files with 16 additions and 6 deletions
+3 -1
View File
@@ -57,7 +57,9 @@
"optimization" : {
"subPackages" : true
},
"usingComponents" : true
"usingComponents" : true,
/* ,iOS */
"requiredBackgroundModes" : ["audio"]
},
"mp-alipay" : {
"usingComponents" : true
+2 -1
View File
@@ -122,7 +122,8 @@
"navigationBarBackgroundColor": "#f8fafc",
"navigationBarTextStyle": "black",
"backgroundColor": "#f8fafc",
"disableScroll": true
"disableScroll": true,
"requiredBackgroundModes": ["audio"]
}
}
]
+11 -4
View File
@@ -244,14 +244,21 @@ onShow(() => {
preload()
})
/**
* onHide 故意不 stop:
* - 切到后台 / 微信"小窗" 都会触发 onHide
* - 配合 pages.json 的 requiredBackgroundModes:["audio"],
* 小程序退到后台后节拍声仍然可以继续(健走时把手机放兜里也能听到响)
* - 真正离开节拍器页(navigateBack/重启) 会 onUnload+onUnmounted,
* 在那里清理屏幕常亮
*/
onHide(() => {
if (isPlaying.value) {
stop()
uni.setKeepScreenOn({ keepScreenOn: false })
}
/* 不再主动 stop, 让节拍器在后台继续工作 */
})
onUnmounted(() => {
/* 页面卸载才真正停 + 释放屏幕常亮 */
if (isPlaying.value) stop()
uni.setKeepScreenOn({ keepScreenOn: false })
})
</script>