我是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');
}
发布于 2019-03-27 21:29:39
这行得通..。离子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);
}
}
);
}希望能有所帮助。;)
发布于 2019-08-04 10:36:14
您没有将选项作为参数传递给startListening()。应该是这样的:
start(){
let options ={
language:'en-US'
}
** this.speechRecognition.startListening(options) **
.subscribe(
(matches: Array<string>) => {
console.log(matches);
},
(onerror) => console.log('error:', onerror)
)
}希望这能有所帮助。
https://stackoverflow.com/questions/54838622
复制相似问题