0

我有这个角度服务,通过网络语音将语音翻译成文本

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 = [];


  constructor() { }
  getTranscriptValue()
  {
    return this.transcript_arr;
  }
  getConfidenceValue()
  {
    return this.confidence_arr;
  }
  init() {
    this.recognition.continuous = true;
    this.recognition.interimResults = false;
    this.recognition.maxAlternatives = 1;
    this.recognition.lang = 'en-US';

      this.recognition.addEventListener('result', (e:any) => {
        let last = e.results.length - 1;
        let temp_trans = e.results[last][0].transcript; 
        let confidence = e.results[last][0].confidence; 
        this.confidence_arr.push(confidence);
        this.transcript_arr.push(temp_trans); 
        const transcript = Array.from(e.results)
          .map((result:any) => result[0])
          .map((result) => result.transcript)
          .join(''); 
        this.tempWords = transcript;
      });
    
  
  }

  start() {
    if(this.isListening==false)
    {
      this.isListening = true;
      this.recognition.start();
    }
    
    this.recognition.addEventListener('end', (condition:any) => {
      if (!this.isListening) {
        this.recognition.stop();
      } else {
        this.wordConcat()
        this.recognition.start();
      }
    });
  }
  stop() {
    this.isListening = false;
    this.wordConcat();
    this.recognition.stop();
  }
  reinit()
  { 
    this.transcript_arr=[];
    this.confidence_arr=[];
    this.tempWords='';
    this.text='';
  }
  wordConcat() {
    this.text = this.text + ' ' + this.tempWords + '.';
    this.tempWords = '';
  }
}

我从文件中调用此服务函数,.ts就像我在启动时调用 this.service.init() 一样,然后this.service.start()在此之后我调用this.service.stop()this.service.reinit()但我仍然使用result event listenerintemp_trans变量中的新值获取旧值我不想使用我想要的旧值如果我阻止它,新的价值观this.service.stop()

任何解决方案都受到高度赞赏。谢谢

4

1 回答 1

0

重新启动时也尝试清除temp_trans

@Injectable({
  providedIn: 'root'
})
export class VoiceRecognitionService {

  recognition = new webkitSpeechRecognition();
  isListening = false;
  temp_trans = '';
  public text = '';
  public tempWords: any;
  public transcript_arr: string[] = [];
  public confidence_arr: string[] = [];

  constructor() { }

  getTranscriptValue() {
    return this.transcript_arr;
  }

  getConfidenceValue() {
    return this.confidence_arr;
  }

  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);
      this.tempWords = Array.from(e.results)
                            .map((result: any) => result[0])
                            .map((result) => result.transcript)
                            .join('');
    });
  }

  stopListening() {
    this.recognition.removeEventListener('result', () => ());
  }

  start() {
    if (!this.isListening) {
      this.isListening = true;
      this.recognition.start();
    }

    this.recognition.addEventListener('end', (condition: any) => {
      if (!this.isListening) {
        this.recognition.stop();
      } else {
        this.wordConcat();
        this.recognition.start();
      }
    });
  }

  stop() {
    this.isListening = false;
    this.wordConcat();
    this.recognition.stop();
    this.stopListening();
  }

  reinit() {
    this.startListening();
    this.transcript_arr = [];
    this.confidence_arr = [];
    this.temp_trans = '';
    this.tempWords = '';
    this.text = '';
  }

  wordConcat() {
    this.text = this.text + ' ' + this.tempWords + '.';
    this.tempWords = '';
  }
}
于 2022-01-28T08:45:55.843 回答