【发布时间】: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