You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular 5集成Microsoft Speech API报错:SDK.Recognizer.CreateRecognizer不存在

Angular与Microsoft Speech API集成的问题解决方案

我之前刚好做过Angular和Microsoft Speech API的集成,遇到过类似的问题,给你分享下我的解决思路和实际经验:

关于SDK.Recognizer.CreateRecognizer is not a function错误

这个问题大概率是SDK版本迭代导致的API变更:

  • 你用的microsoft-speech-browser-sdk属于旧版的WebSocket实现,其中CreateRecognizer静态方法在后续版本中被废弃,取而代之的是直接实例化SpeechRecognizer类。
  • 如果你坚持使用这个旧SDK,建议查看仓库的历史版本(比如对应示例代码的版本),或者降级SDK到匹配的版本;更推荐的是改用官方维护的最新语音SDK包。

推荐的集成方案(使用官方最新SDK)

现在微软官方推荐使用microsoft-cognitiveservices-speech-sdk包来做Web端语音集成,它的API更稳定,文档也更完善:

  1. 安装依赖:
    npm install microsoft-cognitiveservices-speech-sdk
    
  2. 在Angular组件中导入并使用:
    import * as speechSDK from 'microsoft-cognitiveservices-speech-sdk';
    import { NgZone } from '@angular/core';
    
    export class SpeechComponent {
      private speechRecognizer: speechSDK.SpeechRecognizer | undefined;
    
      constructor(private ngZone: NgZone) {}
    
      startRecognition() {
        // 配置语音服务
        const speechConfig = speechSDK.SpeechConfig.fromSubscription('你的订阅密钥', '你的区域');
        // 使用默认麦克风作为音频输入
        const audioConfig = speechSDK.AudioConfig.fromDefaultMicrophoneInput();
        
        // 创建识别器实例
        this.speechRecognizer = new speechSDK.SpeechRecognizer(speechConfig, audioConfig);
    
        // 绑定识别事件
        this.speechRecognizer.recognizing = (s, e) => {
          this.ngZone.run(() => {
            // 处理实时识别结果,用NgZone确保Angular视图更新
            console.log(`实时识别: ${e.result.text}`);
          });
        };
    
        this.speechRecognizer.recognized = (s, e) => {
          this.ngZone.run(() => {
            if (e.result.reason === speechSDK.ResultReason.RecognizedSpeech) {
              console.log(`最终识别结果: ${e.result.text}`);
            } else if (e.result.reason === speechSDK.ResultReason.NoMatch) {
              console.log('未识别到有效语音');
            }
          });
        };
    
        // 启动识别
        this.speechRecognizer.startContinuousRecognitionAsync();
      }
    
      stopRecognition() {
        if (this.speechRecognizer) {
          this.speechRecognizer.stopContinuousRecognitionAsync();
        }
      }
    }
    

关键注意事项

  • Zone.js问题:语音SDK的回调事件默认运行在Angular Zone之外,会导致视图不更新,所以需要用NgZone.run()包裹回调逻辑。
  • 麦克风权限:浏览器需要用户授权麦克风访问,要处理权限申请的逻辑(可以通过navigator.mediaDevices.getUserMedia提前检测权限)。
  • API文档:官方的Azure认知服务语音SDK文档包含Web端的详细指引,里面有Angular集成的最佳实践,你可以在Azure官方文档中找到对应内容。

关于旧SDK的API参考

如果一定要继续使用microsoft-speech-browser-sdk,可以查看仓库的docs目录或者历史提交记录,部分旧版本的API文档可能还保留在仓库中;另外也可以查看仓库的issue区,很多开发者都遇到过类似的API变更问题,能找到对应的解决方案。

内容的提问来源于stack exchange,提问作者Adamo Figueroa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:26:23