ionic 4中的语音识别有问题

时间:2019-02-23 05:46:34

标签: javascript ionic-framework speech-recognition

我是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');
    }

2 个答案:

答案 0 :(得分:0)

这有效...离子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);
            }
        }
    );
}

希望有帮助。 ;)

答案 1 :(得分:0)

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

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

希望这会有所帮助。