首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ionic 4中的语音识别问题

ionic 4中的语音识别问题
EN

Stack Overflow用户
提问于 2019-02-23 13:46:34
回答 2查看 1.3K关注 0票数 0

我是ionic的新手,在使用语音识别插件时遇到了麻烦。我使用ionic 4,错误如下。

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

有谁能帮帮我吗?

下面是我的代码:

代码语言:javascript
复制
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');
    }

EN

回答 2

Stack Overflow用户

发布于 2019-03-27 21:29:39

这行得通..。离子4

代码语言:javascript
复制
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);
            }
        }
    );
}

希望能有所帮助。;)

票数 0
EN

Stack Overflow用户

发布于 2019-08-04 10:36:14

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

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

希望这能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54838622

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档