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:
@@ -57,7 +57,9 @@
|
|||||||
"optimization" : {
|
"optimization" : {
|
||||||
"subPackages" : true
|
"subPackages" : true
|
||||||
},
|
},
|
||||||
"usingComponents" : true
|
"usingComponents" : true,
|
||||||
|
/* 节拍器后台播放需要,iOS 必需 */
|
||||||
|
"requiredBackgroundModes" : ["audio"]
|
||||||
},
|
},
|
||||||
"mp-alipay" : {
|
"mp-alipay" : {
|
||||||
"usingComponents" : true
|
"usingComponents" : true
|
||||||
|
|||||||
@@ -122,7 +122,8 @@
|
|||||||
"navigationBarBackgroundColor": "#f8fafc",
|
"navigationBarBackgroundColor": "#f8fafc",
|
||||||
"navigationBarTextStyle": "black",
|
"navigationBarTextStyle": "black",
|
||||||
"backgroundColor": "#f8fafc",
|
"backgroundColor": "#f8fafc",
|
||||||
"disableScroll": true
|
"disableScroll": true,
|
||||||
|
"requiredBackgroundModes": ["audio"]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -244,14 +244,21 @@ onShow(() => {
|
|||||||
preload()
|
preload()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
/**
|
||||||
|
* onHide 故意不 stop:
|
||||||
|
* - 切到后台 / 微信"小窗" 都会触发 onHide
|
||||||
|
* - 配合 pages.json 的 requiredBackgroundModes:["audio"],
|
||||||
|
* 小程序退到后台后节拍声仍然可以继续(健走时把手机放兜里也能听到响)
|
||||||
|
* - 真正离开节拍器页(navigateBack/重启) 会 onUnload+onUnmounted,
|
||||||
|
* 在那里清理屏幕常亮
|
||||||
|
*/
|
||||||
onHide(() => {
|
onHide(() => {
|
||||||
if (isPlaying.value) {
|
/* 不再主动 stop, 让节拍器在后台继续工作 */
|
||||||
stop()
|
|
||||||
uni.setKeepScreenOn({ keepScreenOn: false })
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
onUnmounted(() => {
|
onUnmounted(() => {
|
||||||
|
/* 页面卸载才真正停 + 释放屏幕常亮 */
|
||||||
|
if (isPlaying.value) stop()
|
||||||
uni.setKeepScreenOn({ keepScreenOn: false })
|
uni.setKeepScreenOn({ keepScreenOn: false })
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user