当前位置: 首页 > news >正文

小程序实现语音识别功能

在这里插入图片描述
不废话,直接上代码

<template><view><u-popupround="16" :show="recordShow" :close-on-click-overlay="false":safe-area-inset-bottom="false"@close="close"@open="open"><view class="tag-popup-box"><view class="title"><text>{{ tips }}</text><imagesrc=""@click="close"/></view><view class="voice-box" @touchstart="startRecord" @touchend="stopRecord" @touchcancel="stopRecord"><imagesrc=""class="voice-icon"/></view></view></u-popup></view>
</template>
<script setup lang="ts">
import {onMounted, ref, watch} from "vue";
let start = false
const tips = ref<string>('按住开始录音')
const props = defineProps({show: {type: Boolean,default: false}
})
const manager = ref<any>()
const emits = defineEmits(['update:show','startRecord','textChange','micInput'])const startRecord = async () => {if (start) return // 防止还在识别中时又触发录音console.log('touch started')tips.value = '准备中...'try {await checkPermission()} catch (e) {tips.value = '需要授权'return}manager.value.start()emits("startRecord")
}const stopRecord = () => {if (!start) return // 触发极短时间,stop会在还未start的情况下触发console.log('touch ended or canceled')manager.value.stop()
}watch(() => props.show, (value) => {recordShow.value = value
})const recordShow = ref<boolean>(props.show)
const open = () => {manager.value = requirePlugin("WechatSI").getRecordRecognitionManager()// recordShow.value = truemanager.value.onStart = (e:any) => {console.log('on-start')console.log(e)start = truetips.value = '正在识别...'}manager.value.onStop = (e:any) => {console.log('on-stop')console.log(e)start = falseif (e.result){tips.value = '按住开始录音'emits("micInput", e.result)} else {tips.value = '识别失败,请重试'}}manager.value.onRecognize = (e:any) => {console.log('on-recognize')console.log(e)}manager.value.onError = (e:any) => {console.log('on-error')console.log(e)start = falsetips.value = '识别失败,请重试'}
}
const close = () => {recordShow.value = falseemits('update:show', recordShow.value)
}const  checkPermission = async () => {const sRes = await uni.getSetting()if (sRes.authSetting['scope.record']) returntry {const aRes = await uni.authorize({scope: 'scope.record'})} catch (e) {const mRes = await uni.showModal({title: '授权',content: '请打开 录音功能 权限以便进行语音识别',showCancel: true,})if (mRes.cancel) throw new Error('授权失败')const sRes = await uni.openSetting()if (sRes.authSetting['scope.record']) {uni.showModal({title: '授权成功',content: '请继续点击下方按钮 进行语音输入',showCancel: false})throw new Error('授权成功')}throw new Error('授权失败')}
}
</script>
<style scoped lang="scss">
.tag-popup-box{height: 524rpx;background: #FFFFFF;width: 100%;padding: 40rpx 40rpx 0 ;box-sizing: border-box;border-radius: 32rpx 32rpx 0rpx 0rpx;.voice-box{width: 100%;display: flex;justify-content: center;margin-top: 60rpx;.voice-icon{width: 180rpx;height: 180rpx;}}.title{display: flex;justify-content: space-between;align-items: center;margin-bottom: 60rpx;&>text{font-size: 30rpx;font-family: PingFangSC-Medium, PingFang SC;font-weight: 500;color: #333333;line-height: 44rpx;}&>image{width: 44rpx;height: 44rpx;}}.content{width: 100%;height: 96rpx;background: rgba(153, 153, 153, 0.08);border-radius: 12rpx;padding: 0 26rpx;box-sizing: border-box;}.confirm-btn{margin-top: 138rpx;width: 100%;height: 80rpx;text-align: center;background: #07B780;border-radius: 8rpx;font-size: 28rpx;font-family: PingFangSC-Regular, PingFang SC;font-weight: 400;color: #FFFFFF;line-height: 80rpx;}
}
</style>
http://www.lryc.cn/news/230402.html

相关文章:

  • 判断两层对象中是否有空的value值
  • 【SQLite】环境安装
  • 【QT】飞机大战
  • linux最全基础入门命令(简单明了建议收藏)
  • dgl 的cuda 版本 环境配置(dgl cuda 版本库无法使用问题解决)
  • 回文数和复利的威力(C#)
  • 【Java】面向对象程序设计 课程笔记 Java核心类
  • 16个值得推荐的.NET ORM框架
  • Git 进阶使用
  • 【微软技术栈】C#.NET 泛型数学
  • 【nlp】1.1文本处理的基本方法
  • 流量分析(信息安全铁人三项赛分区赛2-5.18)
  • 云服务器如何选?腾讯云2核2G3M云服务器88元一年!
  • 【Hello Go】初识Go语言
  • 计算机视觉:人脸识别与检测
  • 【NLP】理解 Llama2:KV 缓存、分组查询注意力、旋转嵌入等
  • ctyunos 与 openeuler
  • 跟着GPT学设计模式之工厂模式
  • VScode+python开发,多个解释器切换问题
  • c++ 经典服务器开源项目Tinywebserver如何运行
  • c++之xml的创建,增删改查
  • 【前端开发】JS Vue React中的通用递归函数
  • 【python 生成器 面试必备】yield关键字,协程必知必会系列文章--自己控制程序调度,体验做上帝的感觉 1
  • 头哥实践平台之MapReduce基础实战
  • Linux基础知识——tmux和vim
  • Java Web——TomcatWeb服务器
  • Zookeeper 命令使用和数据说明
  • 索尼RSV文件怎么恢复为MP4视频
  • pytorch-gpu(Anaconda3+cuda+cudnn)
  • 解析数据洁净之道:BI中数据清理对见解的深远影响