Просмотр исходного кода

mallApp 花材增减播放响声

shizhongqi 3 месяцев назад
Родитель
Сommit
41cd875a13
3 измененных файлов с 67 добавлено и 2 удалено
  1. 5 2
      mallApp/src/mixins/cgProduct.js
  2. BIN
      mallApp/src/static/hit.mp3
  3. 62 0
      mallApp/src/utils/util.js

+ 5 - 2
mallApp/src/mixins/cgProduct.js

@@ -490,12 +490,12 @@ export default {
 					}
 				}
 			}
-
+			this.$util.hitRemind();
+			
 			this.setSelectInfoByType({ type: this.pageType, info: list });
 
 			//记忆已经选择的花材
 			this.rememberProduct()
-
 		},
 		addCustomNumEvent(item,params1,type) {
 			const list = this.selectList;
@@ -550,6 +550,9 @@ export default {
 				}
 			}
 			this.setSelectInfoByType({type: this.pageType, info: list});
+			
+			this.$util.hitRemind()
+			
 			//记忆已经选择的花材
 			this.rememberProduct()
 		},

BIN
mallApp/src/static/hit.mp3


+ 62 - 0
mallApp/src/utils/util.js

@@ -273,6 +273,67 @@ export const unique = (arr) => {
 	return arr;
 };
 
+
+// 点击声效 - 缓存对象,避免重复创建
+let hitAudioContext = null
+let lastHitPlayTime = 0
+const HIT_MIN_INTERVAL = 100
+
+const initHitAudio = () => {
+	if (hitAudioContext) return
+	try {
+		if (uni.getSystemInfoSync().platform === 'windows') return
+		
+		hitAudioContext = uni.createInnerAudioContext()
+		hitAudioContext.autoplay = false
+		hitAudioContext.src = '/static/hit.mp3'
+		hitAudioContext.volume = 1
+	} catch (e) {
+		console.error('[hitRemind] 初始化失败:', e)
+		hitAudioContext = null
+	}
+}
+
+export const hitRemind = () => {
+	try {
+		// 防抖:100ms 内只播放一次
+		const now = Date.now()
+		if (now - lastHitPlayTime < HIT_MIN_INTERVAL) {
+			return
+		}
+		
+		// 初始化音频
+		if (!hitAudioContext) {
+			initHitAudio()
+		}
+		
+		if (!hitAudioContext) {
+			return
+		}
+		
+		// 更新时间戳
+		lastHitPlayTime = now
+		
+		// 停止之前的播放
+		try {
+			hitAudioContext.stop()
+		} catch (e) {
+			// 忽略停止失败
+		}
+		
+		// 设置音量并播放
+		hitAudioContext.volume = 1
+		try {
+			hitAudioContext.play()
+		} catch (playError) {
+			console.error('[hitRemind] 播放异常:', playError)
+			hitAudioContext = null
+		}
+	} catch (e) {
+		console.error('[hitRemind] 执行出错:', e)
+	}
+}
+
 //#ifdef APP-PLUS
 const CLDialog=uni.requireNativePlugin("CL-Dialog")
 //#endif
@@ -373,4 +434,5 @@ export default {
 	getCheckLogin,
 	unique,
 	confirmModal,
+	hitRemind,
 }