webkitSpeechRecognition:HTML5语音辨认文字(直接运行)

打印 上一主题 下一主题

主题 919|帖子 919|积分 2757

前端想要实现语音转文字,实在不需要任何云服务,浏览器自带的api就能搞定。
下面是已经封装好的代码,
复制之后可以在控制台只接运行
 
  1. class SpeechRecognitionManager {
  2.     tempTranscript = ''
  3.     isRecording = false;
  4.     timeoutid = 0;
  5.     exitKeywors = ['stop', 'exit', 'quit', '退出', '停止识别', '说完了', '停止', '发送', '好了']
  6.     constructor() {
  7.         this.recognition = new webkitSpeechRecognition();
  8.         this.recognition.continuous = true;
  9.         this.recognition.interimResultsTimeout = 30000;
  10.         this.recognition.lang = 'zh-CN';
  11.         this.recognition.maxSpeechTime = 60000;
  12.         this.resultListeners = [];
  13.         this.recognition.onresult = (event) => {
  14.             const transcript = Array.from(event.results).at(-1)[0].transcript;
  15.             clearTimeout(this.timeoutid);
  16.             if (this.exitKeywors.some(keyword => transcript.includes(keyword) && transcript.length <= 7)) {
  17.                 this.recognition.stop();
  18.                 // this.isRecording = false;
  19.                 // this.notifyResultListeners({
  20.                 //     transcript: this.tempTranscript,
  21.                 //     type: 'end',
  22.                 // });
  23.                 return;
  24.             }
  25.             this.timeoutid = setTimeout(() => {
  26.                 this.recognition.stop();
  27.             }, 4000);
  28.             this.tempTranscript += transcript;
  29.             this.notifyResultListeners({
  30.                 transcript,
  31.                 type: 'tempresult',
  32.             });
  33.         };
  34.         this.recognition.onend = () => {
  35.             this.isRecording = false;
  36.             this.notifyResultListeners({
  37.                 type: 'end',
  38.                 transcript: this.tempTranscript
  39.             });
  40.         };
  41.         this.recognition.onerror = (event) => {
  42.             this.isRecording = false;
  43.             this.notifyResultListeners({
  44.                 type: 'error',
  45.                 transcript: this.tempTranscript,
  46.                 error: event.error
  47.             });
  48.         };
  49.         this.recognition.onstart = () => {
  50.             this.isRecording = true;
  51.             this.tempTranscript = '';
  52.             this.notifyResultListeners({
  53.                 type: 'start'
  54.             });
  55.         };
  56.     }
  57.     addResultListener(listener) {
  58.         this.resultListeners.push(listener);
  59.     }
  60.     notifyResultListeners(data) {
  61.         this.resultListeners.forEach(listener => {
  62.             listener(data);
  63.         });
  64.     }
  65.     startRecognition() {
  66.         if (this.isRecording) return console.log('正在识别');
  67.         console.log('开始识别');
  68.         this.tempTranscript = '';
  69.         this.recognition.start();
  70.     }
  71.     stopRecognition() {
  72.         if (!this.isRecording) return console.log('未开始识别');
  73.         this.isRecording = false;
  74.         console.log('停止识别');
  75.         this.recognition.stop();
  76.     }
  77. }
  78. // 使用示例
  79. window.speechmanager = new SpeechRecognitionManager();
  80. // // 添加结果监听函数
  81. // speechmanager.addResultListener(transcript => {
  82. //     console.log('识别结果:', transcript);
  83. // });
  84. // // 开始识别
  85. // speechmanager.startRecognition();
  86. // // // 停止识别
  87. // // // manager.stopRecognition();
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

西河刘卡车医

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表