[英]Service data loading with delay in angular
I have created this service我创建了这个服务
import { Injectable } from '@angular/core';
declare var webkitSpeechRecognition: any;
@Injectable({
providedIn: 'root'
})
export class VoiceRecognitionService {
recognition = new webkitSpeechRecognition();
isListening = false;
public text = '';
public tempWords : any;
public transcript_arr =[];
public confidence_arr =[];
public temp_trans ='';
constructor() { }
init() {
this.recognition.continuous = true;
this.recognition.interimResults = false;
this.recognition.maxAlternatives = 1;
this.recognition.lang = 'en-US';
this.startListening();
}
startListening() {
this.recognition.addEventListener('result', (e:any) => {
let last = e.results.length - 1;
this.temp_trans = e.results[last][0].transcript;
let confidence = e.results[last][0].confidence;
this.confidence_arr.push(confidence);
this.transcript_arr.push(this.temp_trans);
});
}
start() {
if(this.isListening==false)
{
this.isListening = true;
try{
this.recognition.start();
}
catch(e){
}
}
this.recognition.addEventListener('end', (condition:any) => {
if (!this.isListening) {
this.recognition.stop();
} else {
this.wordConcat();
try{
this.recognition.start();
}
catch(e)
{
}
}
});
}
stopListening() {
this.recognition.removeEventListener('result',null);
}
stop() {
this.isListening = false;
this.wordConcat();
this.recognition.stop();
}
reinit()
{
this.transcript_arr=[];
this.confidence_arr=[];
this.tempWords='';
this.text='';
this.temp_trans='';
}
wordConcat() {
this.text = this.text + ' ' + this.tempWords + '.';
this.tempWords = '';
}
}
using this transcript array this.service.transcript_arr
in .ts
file but the last processing of words are returned with some delay after this service has been stopped, but i want if the output is still pending or the component for showing result is already loaded then it should show the transcript result also在.ts
文件中使用此脚本数组this.service.transcript_arr
但在此服务停止后会延迟返回单词的最后处理,但我希望 output 仍处于挂起状态或用于显示结果的组件已加载然后它也应该显示成绩单结果
What about making you transcript_arr
to an observable - that way you always get the latest value even if it arrives with a delay.让你的transcript_arr
变成一个 observable 怎么样 - 这样你总是能得到最新的值,即使它延迟到达。 I am using ReplaySubject
with bufferSize of 1 here so that when we subscribe, we only get the last value when we have something to show.我在这里使用了缓冲区大小为 1 的ReplaySubject
,这样当我们订阅时,我们只有在有东西要显示时才能得到最后一个值。
public transcript_arr = new ReplaySubject<[]>(1);
startListening() {
this.recognition.addEventListener('result', (e:any) => {
let last = e.results.length - 1;
this.temp_trans = e.results[last][0].transcript;
let confidence = e.results[last][0].confidence;
this.confidence_arr.push(confidence);
this.transcript_arr.next(this.temp_trans);
});
}
reinit()
{
this.transcript_arr.next([]);
this.confidence_arr=[];
this.tempWords='';
this.text='';
this.temp_trans='';
}
And In your component use it like so:并在您的组件中像这样使用它:
lastestTranscript$: Observable<[]>;
this.lastestTranscript$ = this.service.transcript_arr.pipe(data => {return data;})
And show it in the html like so:并在 html 中显示,如下所示:
<div>{{lastestTranscript$ | async}} </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.