【问题标题】:How to hide an element automatically after completion of recording in kamiazya/ngx-speech-recognition angular version如何在 kamiazya/ngx-speech-recognition 角度版本中完成录制后自动隐藏元素
【发布时间】:2019-11-23 16:36:12
【问题描述】:

我有一个文本框和一个收听按钮,当您单击收听时,它将开始录制并附加到文本框,并且“正在收听..”文本将可见。现在我的要求是,当我在任何时间之间单击“停止收听”按钮时,录音应该停止并且“正在收听..”文本应该被隐藏。还假设我的录音在单击“停止收听..”按钮之前结束,“正在收听”..”文本应在自动完成录音后隐藏。这是我创建的演示 https://stackblitz.com/edit/angular-wyphh6?file=src%2Fapp%2Fapp.component.ts。也请在下面找到代码。

app.component.html

<hello name="{{ name }}"></hello>

<p><input type="text" value="{{message}}"></p>
  <button

    (click)="listen()"
  >listen</button>
<p [hidden]="listening">Listening..</p>
<button [hidden]="listening">Stop Listening</button>

app.component.ts

import { Component } from '@angular/core';
import { RxSpeechRecognitionService, resultList, } from '@kamiazya/ngx-speech-recognition';
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ],
  providers: [ RxSpeechRecognitionService ]
})
export class AppComponent  {
  name = 'Angular';
  message = '';
  listening:boolean = true;
constructor(public service: RxSpeechRecognitionService) {


         }

     listen() {
       this.listening = false;
    this.service
      .listen()
      .pipe(resultList)
      .subscribe((list: SpeechRecognitionResultList) => {
        this.message = list.item(0).item(0).transcript;
        console.log('RxComponent:onresult', this.message, list);

      });

  }
  }

【问题讨论】:

  • 你应该在你的 stackblitz 项目中添加 @kamiazya/ngx-speech-recognition 作为包依赖
  • 它还依赖于@angular/cdk。还值得一提的是,它仅适用于 chrome。
  • 我已经添加了这个包,它工作正常,但我的要求是当我在任何时候点击“停止收听”按钮时,录音应该停止并且“正在收听..”文本应该被隐藏。还假设我的录音在单击“停止收听..”按钮之前结束,“正在收听”..'文本应在自动完成录音后隐藏

标签: angular typescript


【解决方案1】:

我已经分叉了你的example。要停止收听,请将 takeUntil [operator] (https://www.learnrxjs.io/operators/filtering/takeuntil.html) 添加到管道。使用布尔值subject 销毁订阅。所以你的 app.component ts 变成了这样:

  ....
  stop = new Subject<boolean>();      
  listen() {
    this.listening = true;
    this.service
      .listen()
      .pipe(
        resultList,
        takeUntil(this.stop)
      )
      .subscribe((list: SpeechRecognitionResultList) => {
        const item = list.item(0);
        this.message = item.item(0).transcript;
        console.log("RxComponent:onresult", this.message, list);
        if (item.isFinal) { // this is set true when stream ends
          this.listening = false; // so its good time to set false
        }
      });
  }

  stopListening() {
    this.listening = false;
    this.stop.next(true);
  }

在您的视图中使用 *ngIfs 来隐藏和显示视图的必要部分:

  <button  
    (click)="listen()"
  >listen</button>
<p *ngIf="listening">Listening..</p>
<button *ngIf="listening" (click)="stopListening()">Stop Listening</button>

【讨论】:

  • 感谢您的回答。这里有一个小问题,一旦我单击“停止收听”按钮并再次单击“收听”按钮,录音就没有发生。
  • @UIAPPDEVELOPER 似乎当您取消订阅 observable 时比 listen() 方法提供的,它会导致错误并且无法再次订阅它。
  • 那么解决办法是什么
猜你喜欢
  • 2022-10-17
  • 1970-01-01
  • 1970-01-01
  • 2018-04-23
  • 1970-01-01
  • 2015-11-11
  • 1970-01-01
  • 2019-08-08
  • 1970-01-01
相关资源
最近更新 更多