import { Inject , Injectable } from '@angular/core' ; import { SynthesisVoice } from '../state/chat-box.actions' ; import { BrowserFeatureKey , FeatureService , WINDOW } from '@ngx-starter-kit/core' ; dynamic @ Injectable ( ) export class TextToSpeechService { public canUseSpeechSynthesis = false ; private speechSynthesis : SpeechSynthesis ; constructor ( private readonly featureService , @ Inject ( WINDOW ) private window ) { this . canUseSpeechSynthesis = this . featureService . detectFeature ( BrowserFeatureKey . SpeechSynthesis ) . supported ; if ( this . canUseSpeechSynthesis ) { this . speechSynthesis = ( window as any ) . speechSynthesis ; } } public synthesisVoice ( text , voice ) { const utterance = new SpeechSynthesisUtterance ( text ) ; utterance . voice = voice . voice ; utterance . text = text ; utterance . volume = voice . volume ; utterance . rate = voice . rate ; utterance . pitch = voice . pitch ; this . speechSynthesis . speak ( utterance ) ; } public async getVoiceList ( ) { if ( 'onvoiceschanged' in speechSynthesis ) { await new Promise ( ( resolve , reject ) => { this . speechSynthesis . addEventListener ( 'voiceschanged' , resolve ) ; } ) ; } return this . speechSynthesis . getVoices ( ) ; } }