【问题标题】:Trouble with speech recognition in ionic 4ionic 4中的语音识别问题
【发布时间】:2019-08-04 10:36:13
【问题描述】:

我是 ionic 新手,在使用语音识别插件时遇到了问题。我正在使用离子 4 错误如下。

TypeError: Object(...) 不是函数 在 SpeechRecognition.startListening

谁能帮帮我。

这是我的代码:

import { SpeechRecognition } from '@ionic-native/speech-recognition/ngx';

getPermisson(){
  // Check feature available
  this.speechRecognition.hasPermission()
    .then((hasPermission: boolean) => {
      if(!permission){
          this.speechRecognition.requestPermission()
            .then(
              () => console.log('Granted'),
              () => console.log('Denied')
            )
        }
      });
    }

    start(){
      let options ={
        language:'en-US'
      }
      this.speechRecognition.startListening()
      .subscribe(
        (matches: Array<string>) => {
          console.log(matches);
        },
        (onerror) => console.log('error:', onerror)
      )
    }

    active(){
      console.log('active');
    }

    stop(){
      this.speechRecognition.stopListening();
      console.log('Finished recording');
    }

【问题讨论】:

  • 我实际上已经尝试过使用相同的代码和他的指南。在他的案例中,他说他使用的是 ionic 3,而他的语音识别插件是 5.0,所以不匹配。但我使用的是支持语音识别 5.0 插件的 ionic 4。然而,错误不断弹出。
  • 澄清一下,您是否在 app.module.ts 中导入 SpeechRecognition 并将其添加到提供程序数组中?

标签: javascript ionic-framework speech-recognition


【解决方案1】:

这行得通……离子 4

ngOnInit(): void {
    this.hasPermission();
    this.getSupportedLanguages();
    this.startListening();
}

hasPermission(): void {
    this.speechRecognition
        .hasPermission()
        .then((hasPermission: boolean) => {
            if (!hasPermission) {
                this.speechRecognition
                    .requestPermission()
                    .then(
                        onfulfilled => console.log('Granted', onfulfilled),
                        onerror => console.error('Denied', onerror)
                    );
            }
        });
}

// Fails on Android 8.1
// https://issuetracker.google.com/issues/73044965
getSupportedLanguages(): void {
    // Get the list of supported languages
    this.speechRecognition
        .getSupportedLanguages()
        .then(
            (languages: Array<string>) => console.log(languages),
            error => console.log(error)
        );
}

startListening(): void {

    const options: SpeechRecognitionListeningOptions = {
        language: this.preferredLanguage,
        showPartial: true
    };

    this.speechRecognition.startListening(options).subscribe(
        (matches: Array<string>) => {
            console.log('Matches', matches);
            this.zone.run(() => {
                if (matches && matches.length > 0) {
                    this.speechRecognized = matches[0];
                }
            });
        },
        onerror => {
            if (onerror.indexOf('Code=203') !== -1) {
                console.log('speechNotRecognized')
            } else {
                console.error(onerror);
            }
        }
    );
}

希望对您有所帮助。 ;)

【讨论】:

    【解决方案2】:

    您没有将选项作为参数传递给 startListening()。应该是这样的:

    start(){
          let options ={
            language:'en-US'
          }
       ** this.speechRecognition.startListening(options) **
          .subscribe(
            (matches: Array<string>) => {
              console.log(matches);
            },
            (onerror) => console.log('error:', onerror)
          )
        }
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多