UNPKG

@muguilin/xf-voice-dictation

Version:

讯飞语音听写 WebAPI

78 lines (55 loc) 2.88 kB
# @muguilin/xf-voice-dictation [![MIT License](https://img.shields.io/npm/l/utilsjs.svg?style=flat-square)](https://www.npmjs.com/package/mu-tooljs) > 基于 讯飞语音听写流式接口 进行二次封装,用于在 1 分钟内的即时语音转文字技术,支持实时返回识别结果,达到一边上传音频一边获得识别文本的效果。 > > 该语音能力是通过 Websocket API 的方式给开发者提供一个通用的接口。Websocket API 具备流式传输能力,适用于需要流式数据传输的 AI 服务场景,比如边说话边识别。相较于 SDK,API 具有轻量、跨语言的特点;相较于 HTTP API,Websocket API 协议有原生支持跨域的优势。 #### 🏡 下载安装: ```shell # 使用npm命令下载安装 $ npm i @muguilin/xf-voice-dictation # 使用yarn命令下载安装 $ yarn add @muguilin/xf-voice-dictation ``` #### 🔍 实例效果: [语音听写 DEMO(流式版)WebAPI (muguilin.github.io)](https://muguilin.github.io/VoiceDictation/) [讯飞语音听写(流式版)WebAPI 文档 | (xfyun.cn)](https://www.xfyun.cn/doc/asr/voicedictation/API.html) #### 🚀 使用方法: > 【关于:】服务接口认证信息这 3 个参数据:APPID、APISecret、APIKey,请到官网申请(https://www.xfyun.cn/services/voicedictation) > > 【注意:】APISecret 和 APIKey 的长度都差不多很相似,所以要填错哦! ```javascript import { XfVoiceDictation } from '@muguilin/xf-voice-dictation'; let times = null; const xfVoice = new XfVoiceDictation({ APPID: 'xxx', APISecret: 'xxx', APIKey: 'xxx', // webSocket请求地址 非必传参数,默认为:wss://iat-api.xfyun.cn/v2/iat // url: '', // 监听录音状态变化回调 onWillStatusChange: function (oldStatus, newStatus) { // 可以在这里进行页面中一些交互逻辑处理:注:倒计时(语音听写只有60s),录音的动画,按钮交互等! console.log('识别状态:', oldStatus, newStatus); }, // 监听识别结果的变化回调 onTextChange: function (text) { // 可以在这里进行页面中一些交互逻辑处理:如将文本显示在页面中 console.log('识别内容:',text) // 如果3秒钟内没有说话,就自动关闭(60s后也会自动关闭) if (text) { clearTimeout(times); times = setTimeout(() => { this.stop(); }, 3000); }; }, // 监听识别错误回调 onError: function(error){ console.log('错误信息:', error) } }); // 给Dom元素加添事件,来调用开始语音识别! // xfVoice.start(); // 给Dom元素加添事件,来调用关闭语音识别! // xfVoice.stop(); ```