|
| 1 | +# voice-input-button |
| 2 | + |
| 3 | +> A vue voice input button component, based on iFLYTEK speech api. |
| 4 | +
|
| 5 | +> 一个基于讯飞语音听写api的语音输入按钮vue组件。使用HTML原生媒体接口,故兼容性依浏览器而定,具体可参考这里 [Can I Use](https://caniuse.com/#search=getUserMedia) |
| 6 | +
|
| 7 | +## Screenshots / 截屏 |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | +## How to use / 如何使用 |
| 15 | + |
| 16 | +### Create APP and get APIKey / 创建一个语音应用 |
| 17 | + |
| 18 | +1. 您需要在讯飞开放平台上创建一个基于语音听写 WebApi 的语音应用:[创建应用](https://console.xfyun.cn/app/create?source=WebAPI) |
| 19 | +2. 为该应用开通语音听写服务 |
| 20 | +3. 获取应用的 **appId**、**APIKey**,这是必须的。 |
| 21 | + |
| 22 | +### Start A Proxy Service / 启动一个代理服务 |
| 23 | + |
| 24 | +由于前端页面无法直接请求讯飞语音接口,您需要搭建一个代理服务将语音听写请求转发至讯飞服务接口。 |
| 25 | +在该项目源码的 **proxy** 目录,我已经为您准备好了一个 node 版代理服务脚本,您可以直接使用。 |
| 26 | + |
| 27 | +### Install / 安装 |
| 28 | + |
| 29 | +```javascript |
| 30 | +npm install voice-input-button |
| 31 | +// or install from github reponsitory |
| 32 | +npm install https://github.com/ferrinweb/voice-input-button.git |
| 33 | + |
| 34 | +// or use yarn / 推荐使用 yarn |
| 35 | +yarn add voice-input-button |
| 36 | +// or install from github reponsitory |
| 37 | +yarn add https://github.com/ferrinweb/voice-input-button.git |
| 38 | +``` |
| 39 | + |
| 40 | +### Import / 引入 |
| 41 | + |
| 42 | +```javascript |
| 43 | +// global import / 全局引入 |
| 44 | +import voiceInputButton from 'voice-input-button' |
| 45 | +Vue.use(voiceInputButton, { |
| 46 | + server: '', // 您的启动的代理服务地址 |
| 47 | + appId: '', // 您申请的语音听写服务应用的ID |
| 48 | + APIKey: '' // 您开通的语音听写服务的 APIKey |
| 49 | +}) |
| 50 | + |
| 51 | +// import on demand in your vue component file. / 按需引入 |
| 52 | +// 在这种引入方式下,您必须通过在组件标签上设置 server、appId、APIKey 属性来配置相关参数 |
| 53 | +import voiceInputButton from 'voice-input-button' |
| 54 | +export default { |
| 55 | + components: { |
| 56 | + voiceInputButton |
| 57 | + }, |
| 58 | + ... |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +### Use and demo / 使用及示例 |
| 63 | +> You can ckeckout this repository and try this demo. |
| 64 | +
|
| 65 | +> 你可以直接检出 voice-input-button 源码到本地,查看示例。 |
| 66 | +
|
| 67 | +```javascript |
| 68 | +<template> |
| 69 | + <div id="app"> |
| 70 | + <div class="result">{{result}}</div> |
| 71 | + <div class="voice-input-button-wrapper"> |
| 72 | + <voice-input-button |
| 73 | + server="您的代理地址" |
| 74 | + appId="您的应用ID" |
| 75 | + APIKey="您开通的语音听写服务APIKey" |
| 76 | + @record="showResult" |
| 77 | + @record-start="recordStart" |
| 78 | + @record-stop="recordStop" |
| 79 | + @record-blank="recordNoResult" |
| 80 | + @record-failed="recordFailed" |
| 81 | + color="#fff" |
| 82 | + tipPosition="top" |
| 83 | + > |
| 84 | + <template slot="no-speak">没听清您说的什么</template> |
| 85 | + </voice-input-button> |
| 86 | + </div> |
| 87 | + </div> |
| 88 | +</template> |
| 89 | + |
| 90 | +<script> |
| 91 | +import VoiceInputButton from './lib/voice-input-button' |
| 92 | + |
| 93 | +export default { |
| 94 | + name: 'App', |
| 95 | + components: { |
| 96 | + VoiceInputButton |
| 97 | + }, |
| 98 | + data () { |
| 99 | + return { |
| 100 | + result: '' |
| 101 | + } |
| 102 | + }, |
| 103 | + methods: { |
| 104 | + showResult (text) { |
| 105 | + this.result = text |
| 106 | + }, |
| 107 | + recordStart () { |
| 108 | + console.info('录音开始') |
| 109 | + }, |
| 110 | + recordStop () { |
| 111 | + console.info('录音结束') |
| 112 | + }, |
| 113 | + recordNoResult () { |
| 114 | + console.info('没有录到什么,请重试') |
| 115 | + }, |
| 116 | + recordFailed (error) { |
| 117 | + console.info('识别失败,错误栈:', error) |
| 118 | + } |
| 119 | + } |
| 120 | +} |
| 121 | +</script> |
| 122 | + |
| 123 | +<style> |
| 124 | + html, body { |
| 125 | + margin: 0; |
| 126 | + width: 100%; |
| 127 | + height: 100%; |
| 128 | + } |
| 129 | + *{ |
| 130 | + box-sizing: border-box; |
| 131 | + } |
| 132 | + #app { |
| 133 | + position: absolute; |
| 134 | + top: 100px; |
| 135 | + left: 0; |
| 136 | + right: 0; |
| 137 | + margin: 0 auto; |
| 138 | + width: 400px; |
| 139 | + } |
| 140 | + .voice-input-button-wrapper{ |
| 141 | + width: 42px; |
| 142 | + height: 42px; |
| 143 | + background-color: mediumpurple; |
| 144 | + border-radius: 50%; |
| 145 | + } |
| 146 | + .result{ |
| 147 | + width: 100%; |
| 148 | + padding: 25px; |
| 149 | + border: #e2e2e2 1px solid; |
| 150 | + border-radius: 5px; |
| 151 | + line-height: 2; |
| 152 | + font-size: 16px; |
| 153 | + color: #727272; |
| 154 | + min-height: 24px; |
| 155 | + margin-bottom: 25px; |
| 156 | + } |
| 157 | +</style> |
| 158 | +``` |
| 159 | + |
| 160 | +### Slots / 插槽 |
| 161 | +名称 | 说明 | 默认值 |
| 162 | +|---|---|---| |
| 163 | +recording | 正在录音提示文字,按下按钮时,将显示该录音中提示文本 | 无 |
| 164 | +no-speak | 录音完成但未能识别到有效结果是的提示文本 | 无 |
| 165 | + |
| 166 | +### Attributes / 属性 |
| 167 | +名称 | 说明 | 默认值 |
| 168 | +|---|---|---| |
| 169 | +color | 麦克风按钮及录音中、识别中图标的颜色 | #333 |
| 170 | +tipPosition | 正在录音及未识别提示出现的位置,支持 top/right/left/bottom 四个取值 | top |
| 171 | + |
| 172 | +### Events / 事件 |
| 173 | +名称 | 说明 |
| 174 | +|---|---| |
| 175 | +record | 录音识别完成,事件携带识别结果 |
| 176 | +record-start | 按下按钮开始录音 |
| 177 | +record-stop | 录音结束,开始上传语音数据进行识别 |
| 178 | +record-blank | 录音识别完成,但无识别结果 |
| 179 | +record-failed | 录音识别失败,事件携带错误栈数据 |
| 180 | + |
| 181 | +## Lisence |
| 182 | +MIT Lisence. |
0 commit comments