<script>
const audioContext = uni.createInnerAudioContext() // 播放音频的
export default {
data() {
return {
}
},
onShow() {
this.friendShake()
},
onUnload() {
// 取消监听加速度数据。
uni.offAccelerometerChange(res=>{
console.log(res)
})
},
methods: {
// 摇一摇
friendShake() {
uni.onAccelerometerChange(async (res) => {
// 自己根据自己的力度调节 变量大小 满足自己设定的值后触发自己的逻辑
if (Math.abs(res.x) > 30 || Math.abs(res.y) > 30 || Math.abs(res.z) > 30) {
// 进入判断后 相当于摇一摇成功
audioContext.src = '../../static/images/vidio/shake.mp3' // 播放音频
audioContext.play() // 播放
}
});
},
}
}
</script>
uniapp开发App实现摇一摇功能
最新推荐文章于 2023-11-12 20:52:34 发布