• 小程序实现语音识别功能


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

    <template>
      <view>
        <u-popup
          round="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>
              <image
                src=""
                @click="close"
              />
            </view>
            <view class="voice-box" @touchstart="startRecord" @touchend="stopRecord" @touchcancel="stopRecord">
              <image
                src=""
                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 = true
      manager.value.onStart = (e:any) => {
        console.log('on-start')
        console.log(e)
        start = true
        tips.value = '正在识别...'
      }
      manager.value.onStop = (e:any) => {
        console.log('on-stop')
        console.log(e)
        start = false
        if (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 = false
        tips.value = '识别失败,请重试'
      }
    }
    const close = () => {
      recordShow.value = false
      emits('update:show', recordShow.value)
    }
    
    
    const  checkPermission = async () => {
      const sRes = await uni.getSetting()
      if (sRes.authSetting['scope.record']) return
      try {
        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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
  • 相关阅读:
    webpack原理篇(五十八):实战开发一个简易的webpack
    【AI工具】LM Studio 部署本地llama3以及python调用openai的API与llama3本地服务器进行问答...
    [附源码]Python计算机毕业设计Django物品捎带系统
    如何使用命令行参数?
    django请求生命周期流程图,路由匹配,路由有名无名反向解析,路由分发,名称空间
    路由查找原理
    惊帆JF141心率血氧模块简单使用(STM32标准库代码)
    IDEA这样配置Maven:让你一遍就能学会!
    SpringMVC学习篇(三)
    用浏览器进行web应用测试,你会怎么做?
  • 原文地址:https://blog.csdn.net/weixin_45389051/article/details/134405965