语音识别允许用户使用语音输入文本或命令,从而极大地增强 Web 应用程序中的用户交互。在本教程中,我将向您展示如何将语音识别集成到 Angular 应用程序中,并通过语法支持来增强它。
先决条件
- Angular 的基础知识。
- 已安装 Angular CLI。
- 现有的 Angular 项目或使用 ng new voice-recognition-app 创建一个新项目。
第 1 步:设置语音识别服务
首先,我们需要创建一个服务来处理语音识别。该服务将使用 Web Speech API 的 webkitSpeechRecognition 和 SpeechGrammarList。
创建新服务:
ng 生成语音识别服务
现在,更新生成的语音识别.service.ts:
import { Injectable, NgZone } from '@angular/core'; @Injectable({ providedIn: 'root', }) export class SpeechRecognitionService { recognition: any; isListening: boolean = false; constructor(private zone: NgZone) { const { webkitSpeechRecognition, webkitSpeechGrammarList }: IWindow = window as any; this.recognition = new webkitSpeechRecognition(); this.recognition.continuous = false; this.recognition.interimResults = false; this.recognition.lang = 'en-US'; const grammar = '#JSGF V1.0; grammar colors; public <color> = red | green | blue | yellow ;'; const speechRecognitionList = new webkitSpeechGrammarList(); speechRecognitionList.addFromString(grammar, 1); this.recognition.grammars = speechRecognitionList; } startListening(): Promise<string> { return new Promise((resolve, reject) => { if (this.isListening) { reject('Already listening'); } this.isListening = true; this.recognition.onresult = (event: any) => { this.zone.run(() => { const result = event.results[0][0].transcript; this.stopListening(); resolve(result); }); }; this.recognition.onerror = (event: any) => { this.zone.run(() => { this.isListening = false; reject(event.error); }); }; this.recognition.onend = () => { this.zone.run(() => { this.isListening = false; }); }; this.recognition.start(); }); } stopListening(): void { if (this.isListening) { this.recognition.stop(); this.isListening = false; } } } interface IWindow extends Window { webkitSpeechRecognition: any; webkitSpeechGrammarList: any; } </string></color>
第 2 步:创建组件
接下来,我们将创建一个组件来利用我们的语音识别服务。该组件将有一个文本区域和一个麦克风图标。当用户单击图标时,语音识别将开始,识别的文本将添加到文本区域。
更新app.component.ts:
import { Component } from '@angular/core'; import { SpeechRecognitionService } from './speech-recognition.service'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'], }) export class AppComponent { note: string = ''; isListening: boolean = false; constructor(private speechRecognitionService: SpeechRecognitionService) {} toggleListening() { if (this.isListening) { this.speechRecognitionService.stopListening(); this.isListening = false; } else { this.isListening = true; this.speechRecognitionService.startListening().then( (result: string) => { this.note = result; this.isListening = false; }, (error: any) => { console.error('Speech recognition error', error); this.isListening = false; } ); } } }
第 3 步:更新模板
在模板中,我们将点击事件绑定到我们的切换方法,并使用 Angular 的 ngClass 指令在麦克风监听时添加发光效果。
更新app.component.html:
<div class="textarea-container"> <textarea maxlength="150" class="form-control" id="message-text" rows="3"></textarea> <i class="mdi mdi-microphone mic-icon" islistening></i> </div>
第 4 步:添加样式
添加一些样式来定位麦克风图标并在收听时添加发光效果。
更新app.component.css:
.textarea-container { position: relative; display: inline-block; } .textarea-container textarea { width: 100%; box-sizing: border-box; } .mic-icon { position: absolute; top: 10px; right: 10px; cursor: pointer; font-size: 24px; color: #333; transition: box-shadow 0.3s ease; } .mic-icon.glow { box-shadow: 0 0 10px 2px rgba(255, 0, 0, 0.8); }
第 5 步:测试您的应用程序
使用以下命令运行 Angular 应用程序:
服务
在浏览器中导航至 http://localhost:4200/。您应该看到带有麦克风图标的文本区域。当您单击该图标时,它将开始监听,并且该图标会发光。说出语法列表中的一种颜色(红、绿、蓝、黄),识别出的颜色将添加到文本区域。
结论
您已成功将具有语法支持的语音识别添加到您的 Angular 应用程序中。可以扩展此功能以识别更复杂的命令并与应用程序中的各种功能无缝集成。尝试不同的语法和语音识别设置,看看什么最适合您的用例。
在 linkedin 上关注我
快乐编码!
以上是将具有语法支持的语音识别添加到您的 Angular 应用程序中的详细内容。更多信息请关注PHP中文网其他相关文章!

Python和JavaScript在社区、库和资源方面的对比各有优劣。1)Python社区友好,适合初学者,但前端开发资源不如JavaScript丰富。2)Python在数据科学和机器学习库方面强大,JavaScript则在前端开发库和框架上更胜一筹。3)两者的学习资源都丰富,但Python适合从官方文档开始,JavaScript则以MDNWebDocs为佳。选择应基于项目需求和个人兴趣。

从C/C 转向JavaScript需要适应动态类型、垃圾回收和异步编程等特点。1)C/C 是静态类型语言,需手动管理内存,而JavaScript是动态类型,垃圾回收自动处理。2)C/C 需编译成机器码,JavaScript则为解释型语言。3)JavaScript引入闭包、原型链和Promise等概念,增强了灵活性和异步编程能力。

不同JavaScript引擎在解析和执行JavaScript代码时,效果会有所不同,因为每个引擎的实现原理和优化策略各有差异。1.词法分析:将源码转换为词法单元。2.语法分析:生成抽象语法树。3.优化和编译:通过JIT编译器生成机器码。4.执行:运行机器码。V8引擎通过即时编译和隐藏类优化,SpiderMonkey使用类型推断系统,导致在相同代码上的性能表现不同。

JavaScript在现实世界中的应用包括服务器端编程、移动应用开发和物联网控制:1.通过Node.js实现服务器端编程,适用于高并发请求处理。2.通过ReactNative进行移动应用开发,支持跨平台部署。3.通过Johnny-Five库用于物联网设备控制,适用于硬件交互。

我使用您的日常技术工具构建了功能性的多租户SaaS应用程序(一个Edtech应用程序),您可以做同样的事情。 首先,什么是多租户SaaS应用程序? 多租户SaaS应用程序可让您从唱歌中为多个客户提供服务

本文展示了与许可证确保的后端的前端集成,并使用Next.js构建功能性Edtech SaaS应用程序。 前端获取用户权限以控制UI的可见性并确保API要求遵守角色库

JavaScript是现代Web开发的核心语言,因其多样性和灵活性而广泛应用。1)前端开发:通过DOM操作和现代框架(如React、Vue.js、Angular)构建动态网页和单页面应用。2)服务器端开发:Node.js利用非阻塞I/O模型处理高并发和实时应用。3)移动和桌面应用开发:通过ReactNative和Electron实现跨平台开发,提高开发效率。

JavaScript的最新趋势包括TypeScript的崛起、现代框架和库的流行以及WebAssembly的应用。未来前景涵盖更强大的类型系统、服务器端JavaScript的发展、人工智能和机器学习的扩展以及物联网和边缘计算的潜力。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

Atom编辑器mac版下载
最流行的的开源编辑器

记事本++7.3.1
好用且免费的代码编辑器

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

WebStorm Mac版
好用的JavaScript开发工具