## 主要改动 ### 1. 音效集成 - 集成 4 个捏碎音效(鸡蛋/核桃/易拉罐/气球) - 集成背景音乐 BGM + 右上角开关按钮 - 修复音效 URL 空值时的崩溃问题 - useMetronome 新增 volume 参数(默认 1.0) - 节拍音量调小至 0.35,避免木鱼声过大 ### 2. 画布升级到 Canvas 2D 接口 - 解决微信小程序 canvas-id 接口的警告 - 支持同层渲染,可与其他元素正常叠加 - 使用 requestAnimationFrame 替代 setTimeout - DPR 自适应,Retina 屏幕高清渲染 ### 3. 粒子特效差异化升级 - 4 种物品各自独立配色和形状 - 🥚 鸡蛋: 蛋黄黄+白色,圆形,中速 - 🌰 核桃: 4 种棕色,方形/三角/长方形,重粒 - 🥫 易拉罐: 5 种银色,长方形,快速 - 🎈 气球: 8 色彩虹,小颗粒,飘浮(低重力) - 新增双层冲击波效果 - 粒子旋转动画(方形/三角形/长方形) - 多形状粒子绘制(circle/square/triangle/rect) ### 4. 沉浸式画布重新设计 - 画布占屏幕 60vh(min 720rpx, max 900rpx) - 背景与页面无缝融合(#f8fafc) - 中心 1400rpx 大放射光晕,营造空间感 - 握力环放大至 520rpx,内圈 280rpx - 完全移除浮动气泡(性能优化) ### 5. 视觉与体验优化 - 主题色改回青绿色(#14b8a6),握力环保持粉色突出 - 训练完成卡片大改造: - 大号 emoji 跳动动画 - 统计项加入图标(💪⏱️🔥) - 食物对照可视化(emoji + 数量,最多 3 个) - 食物库扩展到 7 种(糖果/饼干/巧克力/苹果/香蕉/米饭/可乐) - BPM 数字大号显示(80rpx 深粉色) - 实时统计栏增加分隔线和顶部彩色高光 ### 6. 性能优化(低配机器友好) - 静态背景渐变(0 性能消耗) - 单层中心光晕(无动画) - 外发光仅在播放时启用 - GPU 加速(will-change + transform/opacity) - 移除 backdrop-filter(高消耗) ### 7. Bug 修复 - crush-canvas 的 \$scope of null 错误 - 进入页面时音效预热不再发声(静音预热) - 修复 walnut emoji 错用花生(🥜→🌰) - 弹窗遮挡粒子动画(延迟 1200ms 显示弹窗) ### 8. 文档 - 新增音效采集清单(grip-ring-audio-shopping-list.md) ## 修改文件 - grip-ring.vue: 主页面,音效集成、UI 重设计、食物对照 - grip-ring-canvas.vue: 沉浸式画布、握力环主体 - crush-canvas.vue: Canvas 2D 升级、差异化粒子系统 - useMetronome.ts: 音量参数支持 - dev-training-entry/index.vue: 入口配置微调 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
276 lines
7.5 KiB
TypeScript
276 lines
7.5 KiB
TypeScript
import { ref, computed, onUnmounted } from 'vue'
|
|
|
|
export interface MetronomeOptions {
|
|
initialBpm?: number
|
|
bpmMin?: number
|
|
bpmMax?: number
|
|
clickSrc?: string
|
|
accentSrc?: string
|
|
accentEvery?: number
|
|
poolSize?: number
|
|
volume?: number // 节拍音音量 0-1
|
|
onBeat?: (beatIndex: number, isAccent: boolean) => void
|
|
}
|
|
|
|
/**
|
|
* 音频实例池:每个 click 用一个独立 InnerAudioContext,循环复用。
|
|
* 解决两个问题:
|
|
* 1. seek+play 模式在某些设备上首拍冷启动延迟(100-300ms)
|
|
* 2. BPM 偏快时上一拍音频还没播完,下一拍 play 会被阻塞或丢失
|
|
*/
|
|
class AudioPool {
|
|
private list: UniApp.InnerAudioContext[] = []
|
|
private cursor = 0
|
|
private warmedUp = false
|
|
private targetVolume = 1
|
|
|
|
constructor(
|
|
private src: string,
|
|
private size: number,
|
|
volume = 1,
|
|
) {
|
|
this.targetVolume = volume
|
|
}
|
|
|
|
private create() {
|
|
for (let i = 0; i < this.size; i++) {
|
|
const ctx = uni.createInnerAudioContext()
|
|
ctx.src = this.src
|
|
ctx.obeyMuteSwitch = false
|
|
ctx.autoplay = false
|
|
ctx.volume = this.targetVolume
|
|
this.list.push(ctx)
|
|
}
|
|
}
|
|
|
|
setVolume(volume: number) {
|
|
this.targetVolume = Math.max(0, Math.min(1, volume))
|
|
this.list.forEach((ctx) => {
|
|
try {
|
|
ctx.volume = this.targetVolume
|
|
} catch (_) {}
|
|
})
|
|
}
|
|
|
|
/**
|
|
* 预热:短暂播放,让音频文件下载/解码到内存
|
|
* 真正首次 play 不会再卡冷启动
|
|
*/
|
|
warmUp() {
|
|
if (this.warmedUp) return
|
|
if (this.list.length === 0) this.create()
|
|
|
|
this.list.forEach((ctx) => {
|
|
try {
|
|
ctx.volume = 0 // 静音预热,避免进入页面时响声
|
|
ctx.play()
|
|
setTimeout(() => {
|
|
try {
|
|
ctx.stop()
|
|
ctx.volume = this.targetVolume // 恢复目标音量
|
|
} catch (_) {}
|
|
}, 60)
|
|
} catch (_) {}
|
|
})
|
|
this.warmedUp = true
|
|
}
|
|
|
|
play() {
|
|
if (this.list.length === 0) {
|
|
this.create()
|
|
this.warmUp()
|
|
}
|
|
const ctx = this.list[this.cursor]
|
|
this.cursor = (this.cursor + 1) % this.list.length
|
|
try {
|
|
// 强制停止所有正在播放的音频,避免重叠
|
|
this.list.forEach(c => {
|
|
try { c.stop() } catch (_) {}
|
|
})
|
|
// iOS 上 seek(0) + play() 不一定能从头播放;stop() + play() 更稳
|
|
ctx.play()
|
|
} catch (_) {
|
|
try { ctx.play() } catch (__) {}
|
|
}
|
|
}
|
|
|
|
destroy() {
|
|
this.list.forEach((ctx) => {
|
|
try {
|
|
ctx.destroy?.()
|
|
} catch (_) {}
|
|
})
|
|
this.list = []
|
|
this.warmedUp = false
|
|
}
|
|
}
|
|
|
|
/* InnerAudio 兼容本地路径和网络 URL,这里默认走 CDN,跟 BgAudio 保持一致便于维护
|
|
首次播放会下载 ~3KB,实测 100~300ms 完成,然后小程序会缓存,后续触发零延迟 */
|
|
const DEFAULT_CLICK_SRC =
|
|
'https://gz-1349751149.cos.ap-guangzhou.myqcloud.com/uploads/file/20260526/20260526105128557ef8669.mp3'
|
|
|
|
export function useMetronome(options: MetronomeOptions = {}) {
|
|
const {
|
|
initialBpm = 80,
|
|
bpmMin = 40,
|
|
bpmMax = 240,
|
|
clickSrc = DEFAULT_CLICK_SRC,
|
|
accentSrc,
|
|
poolSize = 4,
|
|
volume = 1,
|
|
onBeat,
|
|
} = options
|
|
|
|
const bpm = ref<number>(initialBpm)
|
|
const isPlaying = ref<boolean>(false)
|
|
const beatIndex = ref<number>(0)
|
|
const isAccent = ref<boolean>(false)
|
|
const accentEveryRef = ref<number>(options.accentEvery ?? 4)
|
|
|
|
const intervalMs = computed(() => 60000 / bpm.value)
|
|
|
|
let clickPool: AudioPool | null = null
|
|
let accentPool: AudioPool | null = null
|
|
let timer: ReturnType<typeof setTimeout> | null = null
|
|
let nextTickAt = 0
|
|
|
|
const ensureAudio = () => {
|
|
if (!clickPool) {
|
|
clickPool = new AudioPool(clickSrc, poolSize, volume)
|
|
clickPool.warmUp()
|
|
}
|
|
if (accentSrc && !accentPool) {
|
|
accentPool = new AudioPool(accentSrc, Math.max(2, Math.ceil(poolSize / 2)), volume)
|
|
accentPool.warmUp()
|
|
}
|
|
}
|
|
|
|
const playSound = (accent: boolean) => {
|
|
if (accent && accentPool) {
|
|
accentPool.play()
|
|
} else if (clickPool) {
|
|
clickPool.play()
|
|
}
|
|
}
|
|
|
|
const tick = () => {
|
|
if (!isPlaying.value) return
|
|
|
|
const every = Math.max(1, accentEveryRef.value)
|
|
const accent = beatIndex.value % every === 0
|
|
isAccent.value = accent
|
|
|
|
playSound(accent)
|
|
onBeat?.(beatIndex.value, accent)
|
|
beatIndex.value++
|
|
|
|
nextTickAt += intervalMs.value
|
|
const nextDelay = Math.max(0, nextTickAt - Date.now())
|
|
|
|
timer = setTimeout(tick, nextDelay)
|
|
}
|
|
|
|
const start = () => {
|
|
if (isPlaying.value) return
|
|
ensureAudio()
|
|
isPlaying.value = true
|
|
beatIndex.value = 0
|
|
nextTickAt = Date.now()
|
|
tick()
|
|
}
|
|
|
|
const stop = () => {
|
|
isPlaying.value = false
|
|
if (timer) {
|
|
clearTimeout(timer)
|
|
timer = null
|
|
}
|
|
}
|
|
|
|
const setBpm = (val: number) => {
|
|
bpm.value = Math.max(bpmMin, Math.min(bpmMax, Math.round(val)))
|
|
}
|
|
|
|
const setAccentEvery = (n: number) => {
|
|
accentEveryRef.value = Math.max(1, Math.min(16, Math.round(n)))
|
|
beatIndex.value = 0
|
|
}
|
|
|
|
/**
|
|
* 动态更新音频源(用于切换音色)
|
|
*/
|
|
const updateAudioSrc = (newClickSrc: string, newAccentSrc?: string) => {
|
|
const wasPlaying = isPlaying.value
|
|
if (wasPlaying) stop()
|
|
|
|
// 销毁旧的音频池
|
|
clickPool?.destroy()
|
|
accentPool?.destroy()
|
|
clickPool = null
|
|
accentPool = null
|
|
|
|
// 创建新的音频池(静默预热,不播放)
|
|
clickPool = new AudioPool(newClickSrc, poolSize)
|
|
if (newAccentSrc) {
|
|
accentPool = new AudioPool(newAccentSrc, Math.max(2, Math.ceil(poolSize / 2)))
|
|
}
|
|
|
|
// 播放一次预览音效(适中音量)
|
|
const previewCtx = uni.createInnerAudioContext()
|
|
previewCtx.src = newClickSrc
|
|
previewCtx.obeyMuteSwitch = false
|
|
previewCtx.volume = 0.4
|
|
try {
|
|
previewCtx.play()
|
|
setTimeout(() => {
|
|
try {
|
|
previewCtx.destroy?.()
|
|
} catch (_) {}
|
|
}, 500)
|
|
} catch (_) {}
|
|
|
|
// 恢复播放状态
|
|
if (wasPlaying) start()
|
|
}
|
|
|
|
/**
|
|
* 主动预加载(推荐在页面 onShow 时调用,让用户进页面就完成预热)
|
|
* 同时再次设置 setInnerAudioOption,防 App.vue 全局设置失效(iOS 静音键)
|
|
*/
|
|
const preload = () => {
|
|
// #ifdef MP-WEIXIN
|
|
try {
|
|
uni.setInnerAudioOption({
|
|
obeyMuteSwitch: false,
|
|
mixWithOther: true,
|
|
})
|
|
} catch (_) {}
|
|
// #endif
|
|
ensureAudio()
|
|
}
|
|
|
|
onUnmounted(() => {
|
|
stop()
|
|
clickPool?.destroy()
|
|
accentPool?.destroy()
|
|
clickPool = null
|
|
accentPool = null
|
|
})
|
|
|
|
return {
|
|
bpm,
|
|
isPlaying,
|
|
beatIndex,
|
|
isAccent,
|
|
intervalMs,
|
|
accentEvery: accentEveryRef,
|
|
start,
|
|
stop,
|
|
setBpm,
|
|
setAccentEvery,
|
|
updateAudioSrc,
|
|
preload,
|
|
}
|
|
}
|