An Angular directive that wraps the Web Speech API for easy voice input functionality
npm install ngx-speech-buttonbash
npm i -S ngx-speech-button && npm i -D @types/dom-speech-recognition
`
> Note: The @types/dom-speech-recognition package provides TypeScript types for the Web Speech API and is required as a dev dependency.
Compatibility
| Angular Version | Package Version |
| --------------- | --------------- |
| 21.x | 0.0.x |
Browser Support
The Web Speech API is supported in modern browsers. Check Can I Use for current browser support.
Usage
$3
Add the ngxSpeechButton directive to any clickable element to enable voice input.
`typescript
import { Component } from '@angular/core';
import { SpeechButton } from 'ngx-speech-button';
@Component({
selector: 'app-voice-input',
imports: [SpeechButton],
template:
Transcript: {{ transcript }}
,
})
export class VoiceInputComponent {
transcript = '';
onTranscript(text: string) {
this.transcript = text;
}
}
`
$3
Use transcriptChanged to receive updates as the user speaks:
`typescript
@Component({
selector: 'app-live-transcription',
imports: [SpeechButton],
template:
Live: {{ liveText }}
Final: {{ finalText }}
,
})
export class LiveTranscriptionComponent {
liveText = '';
finalText = '';
}
`
$3
Configure the SpeechRecognition API with the config input:
`typescript
@Component({
selector: 'app-custom-speech',
imports: [SpeechButton],
template:
,
})
export class CustomSpeechComponent {
onComplete(text: string) {
console.log('Final:', text);
}
}
`
$3
Handle speech recognition errors with the error output:
`typescript
@Component({
selector: 'app-error-handling',
imports: [SpeechButton],
template:
{{ errorMessage }}
,
})
export class ErrorHandlingComponent {
errorMessage = '';
onComplete(text: string) {
console.log(text);
}
onError(event: SpeechRecognitionErrorEvent) {
this.errorMessage = Error: ${event.error};
}
}
`
$3
For advanced use cases, access the underlying SpeechRecognition instance:
`typescript
@Component({
selector: 'app-advanced',
imports: [SpeechButton],
template: ,
})
export class AdvancedComponent implements AfterViewInit {
@ViewChild('speech') speechButton!: SpeechButton;
ngAfterViewInit() {
// Attach custom event handlers
this.speechButton.recognition?.addEventListener('soundstart', () => {
console.log('Sound detected');
});
this.speechButton.recognition?.addEventListener('speechstart', () => {
console.log('Speech started');
});
}
}
`
$3
Hide the button when the Web Speech API is not available:
`html
ngxSpeechButton
#speech="ngxSpeechButton"
[hidden]="!speech.available()"
(transcriptCompleted)="onComplete($event)"
>
🎤 Voice Input
@if (!speech.available()) {
Voice input not supported in this browser
}
`
API Reference
$3
| Selector | [ngxSpeechButton] |
| -------- | ------------------- |
| Export As | ngxSpeechButton |
| --------- | ----------------- |
#### Inputs
| Name | Type | Default | Description |
| ------ | ------------------------- | ------- | ------------------------------------------------ |
| config | SpeechRecognitionConfig | {} | Configuration options for SpeechRecognition API. |
#### Outputs
| Name | Type | Description |
| ------------------- | ------------------------------------------- | ------------------------------------------------- |
| transcriptChanged | EventEmitter | Emits the current transcript as it updates. |
| transcriptCompleted | EventEmitter | Emits the final transcript when recognition ends. |
| error | EventEmitter | Emits when a speech recognition error occurs. |
#### Properties
| Name | Type | Description |
| ----------- | --------------------------- | ----------------------------------------------------------- |
| available | Signal | Whether the Web Speech API is available. |
| listening | Signal | Whether speech recognition is currently active. |
| recognition | SpeechRecognition \| null | The underlying SpeechRecognition instance for advanced use. |
$3
`typescript
type SpeechRecognitionConfig = Partial<
Pick
>;
`
| Property | Type | Default | Description |
| --------------- | ------------------- | -------------------- | --------------------------------------------- |
| lang | string | navigator.language | Language for recognition (e.g., 'en-US'). |
| continuous | boolean | true | Whether to return continuous results. |
| interimResults | boolean | false | Whether to return interim results. |
| maxAlternatives | number | 1 | Maximum number of alternative transcriptions. |
| grammars | SpeechGrammarList | - | Grammar list to constrain recognition. |
Requirements
- Angular 21+
- Browser with Web Speech API support
Development
To clone this repo and run it locally:
`bash
git clone https://github.com/JayChase/ngx-speech-button.git
cd ngx-speech-button
npm install
npm run build
`
$3
`bash
ng serve demo
`
$3
`bash
npm test
`
License
MIT
Running end-to-end tests
For end-to-end (e2e) testing, run:
`bash
ng e2e
``