当前位置:首页 > VUE

vue实现语音识别demo

2026-02-20 22:15:37VUE

实现语音识别的Vue Demo

在Vue中实现语音识别功能可以通过Web Speech API完成。以下是一个简单的实现方法:

安装依赖 需要安装vue和必要的语音识别库,如annyang(可选)。

npm install vue annyang --save

创建语音识别组件 在Vue组件中集成语音识别功能。

<template>
  <div>
    <button @click="startListening">开始语音识别</button>
    <button @click="stopListening">停止语音识别</button>
    <p>识别结果: {{ transcript }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      transcript: '',
      recognition: null
    }
  },
  mounted() {
    this.initSpeechRecognition()
  },
  methods: {
    initSpeechRecognition() {
      const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition
      this.recognition = new SpeechRecognition()
      this.recognition.continuous = true
      this.recognition.interimResults = true

      this.recognition.onresult = (event) => {
        let interimTranscript = ''
        let finalTranscript = ''

        for (let i = event.resultIndex; i < event.results.length; i++) {
          const transcript = event.results[i][0].transcript
          if (event.results[i].isFinal) {
            finalTranscript += transcript
          } else {
            interimTranscript += transcript
          }
        }

        this.transcript = finalTranscript || interimTranscript
      }

      this.recognition.onerror = (event) => {
        console.error('语音识别错误:', event.error)
      }
    },
    startListening() {
      this.recognition.start()
    },
    stopListening() {
      this.recognition.stop()
    }
  }
}
</script>

使用annyang简化实现 如果希望简化实现,可以使用annyang库:

<template>
  <div>
    <button @click="startListening">开始语音识别</button>
    <p>识别结果: {{ transcript }}</p>
  </div>
</template>

<script>
import annyang from 'annyang'

export default {
  data() {
    return {
      transcript: ''
    }
  },
  methods: {
    startListening() {
      if (annyang) {
        annyang.addCallback('result', (phrases) => {
          this.transcript = phrases[0]
        })
        annyang.start()
      }
    }
  }
}
</script>

注意事项

  • 确保浏览器支持Web Speech API(Chrome、Edge等现代浏览器支持)
  • 需要用户授权麦克风权限
  • 在HTTPS环境下工作更可靠
  • 移动设备上可能有额外限制

扩展功能 可以添加语音命令处理:

vue实现语音识别demo

methods: {
  addCommands() {
    const commands = {
      'hello': () => {
        this.transcript = '你好!'
      },
      'search *term': (term) => {
        this.transcript = `正在搜索: ${term}`
      }
    }
    annyang.addCommands(commands)
  }
}

以上代码提供了在Vue中实现基本语音识别功能的方法,可以根据需求进一步扩展和完善。

标签: 语音识别vue
分享给朋友:

相关文章

vue实现前端注册

vue实现前端注册

Vue 实现前端注册功能 注册表单设计 使用 Vue 的模板语法创建注册表单,包含用户名、邮箱、密码和确认密码字段。表单需绑定 v-model 实现双向数据绑定。 <template>…

vue实现图库

vue实现图库

Vue 实现图库的基本方法 使用 Vue 实现图库功能可以通过多种方式完成,以下是一些常见的方法和实现步骤。 使用 Vue 和第三方库(如 Vue Gallery) 安装 vue-gallery 或…

vue实现动态

vue实现动态

Vue 实现动态路由的方法 在 Vue 中实现动态路由通常涉及以下几个关键步骤: 使用 Vue Router 的动态路由匹配 通过 Vue Router 的 :param 语法可以实现动态路由匹配。…

vue实现复选

vue实现复选

Vue 实现复选框 在 Vue 中实现复选框可以通过 v-model 指令绑定数据,同时结合 input 元素的 type="checkbox" 属性来实现。以下是几种常见的实现方式: 单个复选框…

vue 实现轮播

vue 实现轮播

Vue 实现轮播的方法 使用第三方库(如 Swiper) 安装 Swiper 库: npm install swiper 在 Vue 组件中引入 Swiper: <template>…

vue 实现聊天

vue 实现聊天

使用 Vue 实现聊天功能 创建 Vue 项目并安装依赖 确保已安装 Vue CLI,通过以下命令创建新项目: vue create chat-app 进入项目目录后,安装必要的依赖(如 Socke…