Проблема с распознаванием речи в ионном 4 - PullRequest
0 голосов
/ 23 февраля 2019

Я новичок в ionic, и у меня возникли проблемы с плагином распознавания речи.Я использую 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 голосов
/ 04 августа 2019

Вы не передаете параметры в качестве аргумента для startListening ().Это должно быть так:

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

Надеюсь, это поможет.

0 голосов
/ 27 марта 2019

Это работает ... ionic 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);
            }
        }
    );
}

Надеюсь, это поможет.;)

...