Appearance
Microsoft Azure 浏览器端实时语音识别(LAT/ASR)工具库,基于 microsoft-cognitiveservices-speech-sdk 实现麦克风连续识别、自动结束控制和识别文本事件。
安装
bash
pnpm add @mingto/microsoft-lat快速开始
typescript
import microsoftLat from '@mingto/microsoft-lat'
microsoftLat.config({
subscriptionKey: 'your-subscription-key',
region: 'eastasia',
})
const latInstance = microsoftLat.create(
{
language: 'zh-CN',
},
{
autoControl: true,
initialDelay: 3500,
subsequentDelay: 3000,
}
)
latInstance
.on('appResponseText', (text) => {
console.log('实时识别:', text)
})
.on('appResultText', (text) => {
console.log('最终结果:', text)
})
.on('appFinish', () => {
console.log('识别结束')
})
latInstance.start()
setTimeout(() => {
latInstance.end()
}, 5000)API
microsoftLat.config(systemConfig)
配置 Azure 语音服务参数,返回当前模块对象,支持链式调用。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| subscriptionKey | string | 是 | Azure 语音服务订阅密钥 |
| region | string | 是 | Azure 语音服务区域 |
microsoftLat.create(businessParams?, sectionDelayParams?)
创建语音识别控制器实例。
businessParams
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| language | string | zh-CN | 识别语言区域 |
sectionDelayParams
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| autoControl | boolean | true | 是否自动按静默延迟结束录音 |
| initialDelay | number | 3500 | 首次延迟时间,单位毫秒 |
| subsequentDelay | number | 3000 | 后续延迟时间,单位毫秒 |
实例方法
| 方法 | 说明 |
|---|---|
| start() | 开始连续语音识别 |
| end() | 停止录音,已识别数据继续产出结束事件 |
| on(eventName, callback) | 订阅识别事件,返回当前实例 |
应用事件
| 事件名 | 说明 |
|---|---|
| appResponseText | 实时识别文本更新时触发 |
| appResultText | 最终识别文本产生时触发 |
| appFinish | 应用结束时触发 |
使用示例
typescript
const lat = microsoftLat.create(
{ language: 'zh-CN' },
{ autoControl: false }
)
lat
.on('appResponseText', (text) => {
console.log('实时:', text)
})
.on('appResultText', (text) => {
console.log('完成:', text)
})
.on('appFinish', () => {
console.log('结束')
})
lat.start()
setTimeout(() => {
lat.end()
}, 5000)常用语言代码
| 语言代码 | 说明 |
|---|---|
| zh-CN | 中文,普通话,中国大陆 |
| zh-TW | 中文,台湾 |
| zh-HK | 中文,香港 |
| en-US | 英语,美国 |
| en-GB | 英语,英国 |
| ja-JP | 日语,日本 |
| ko-KR | 韩语,韩国 |
注意事项
subscriptionKey和region来自 Azure 语音服务资源。- 前端代码中不建议直接暴露订阅密钥,生产环境建议通过服务端下发临时配置或代理请求。
- 首次使用需要用户授权麦克风权限。
- 当前实例没有公开
finish()方法,主动结束识别请调用end()。