离子语音识别-运行时错误Object(...)不是hasPermission上的函数 [英] Ionic Speech Recognition - run time error Object(...) is not a function at hasPermission
问题描述
我正在尝试实现一种简单的语音识别服务(暂时单独进行,最终将其添加到更大的应用中).
I'm trying to implement a simple speech recognition service (separately for now, to add it eventually in a bigger app).
我遇到一个奇怪的运行时错误,无法在网上或文档中得到任何答案.
I'm getting a weird run-time error, that I couldn't get any answer to it online or in documentation.
这是代码....
app.module.ts
import { SpeechRecognition } from '@ionic-native/speech-recognition/ngx';
.....
providers: [
StatusBar,
SplashScreen,
SpeechRecognition,
{provide: ErrorHandler, useClass: IonicErrorHandler}]
home.ts
import { SpeechRecognition } from '@ionic-native/speech-recognition/ngx';
import { AlertController } from 'ionic-angular';
......
constructor (private sr: SpeechRecognition, private alertCtrl: AlertController)
......
ngonInit() {
try {
this.sr.hasPermission().then((permission: boolean) => {
if(!permission) {
this.sr.requestPermission().then(
() => console.log("Granted"),
() => console.log("Denied")
);
}
});
}
catch(err) {
let alert = this.alertCtrl.create({
title: "x_x",
message: "Opps! couldn't get permission!",
buttons: [{
text: "cancel",
role: 'cancel',
handler: () => {}
}]
});
console.log(err); //ERROR LOGGED
alert.present();
}
}
start() {
try {
this.sr.startListening().subscribe((matches: Array<string>) => {
console.log(matches);
this.msg= matches[0];
});
}
catch(err) {
let alert = this.alertCtrl.create({
title: "x_x",
message: "Opps! something is wrong!",
buttons: [{
text: "cancel",
role: 'cancel',
handler: () => {}
}]
});
console.log(err); // ERROR LOGGED if ngOnInit kept empty
alert.present();
}
}
带有一个简单的HTML按钮来调用start()&打印识别器匹配的任何内容.
with a simple HTML button to invoke start() & print whatever is getting matched by the recognizer.
如果这是兼容性问题,我正在使用
Just in case this is a compatibility issue, I'm using
@ ionic-native/speech-recognition":"^ 5.0.0"
@ionic-native/speech-recognition": "^5.0.0"
cordova-plugin-speechrecognition":"1.2.0"
cordova-plugin-speechrecognition": "1.2.0"
我遇到的错误总是
TypeError:Object(...)不是SpeechRecognition.hasPermission上的函数
TypeError: Object(...) is not a function at SpeechRecognition.hasPermission
不用说,如果我注释掉ngOnInit的内容,则错误将变为startListening()不是函数.
needless to say, If i comment out the content of ngOnInit, the error will change to startListening() is not a function.
P.S.这是我在StackOverFlow上遇到的第一个问题,希望我提供了有关该问题的足够信息.
P.S. This is my first question here at StackOverFlow, hopefully I provided enough info about the problem.
谢谢.
推荐答案
正如预期的那样,这是一个兼容性(版本)问题.
As expected, it was a compatibility (version) issue.
Ionic 3将运行语音识别,直到:"^ 4.20.0"
Ionic 3 will run speech-recognition up to: "^4.20.0"
/> ionic cordova plugin add cordova-plugin-speechrecognition@4.20.0
/> npm install --save @ionic-native/speech-recognition@4.20.0
import { SpeechRecognition } from '@ionic-native/speech-recognition';
Ionic 4最多可以运行语音识别:"^ 5.0.0"(在撰写本文时为最新版本)
Ionic 4 will run speech-recognition up-to: "^5.0.0" (latest at the time of writing)
/> ionic cordova plugin add cordova-plugin-speechrecognition
/> npm install @ionic-native/speech-recognition
import { SpeechRecognition } from '@ionic-native/speech-recognition/ngx';
P.S.您无法在浏览器上测试该功能.(因为这是cordova插件).
P.S. You can't test that functionality on browser. (since this is a cordova plugin).
在评论中指出"AJT_82"的原因.
Credits to "AJT_82" for pointing this out in the comments.
这篇关于离子语音识别-运行时错误Object(...)不是hasPermission上的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!