|
1 | | -import { isIOS, Observable, Page } from '@nativescript/core'; |
2 | | -import { SpeakOptions, TNSTextToSpeech } from 'nativescript-texttospeech'; |
| 1 | +import { Observable, Page, isIOS } from '@nativescript/core'; |
| 2 | +import { SpeakOptions, TNSTextToSpeech } from '@nativescript-community/texttospeech'; |
3 | 3 |
|
4 | 4 | export class HelloWorldModel extends Observable { |
5 | | - public speakText: string; |
6 | | - private _SpeechSynthesizer: TNSTextToSpeech; |
7 | | - |
8 | | - constructor(mainPage: Page) { |
9 | | - super(); |
10 | | - this.speakText = |
11 | | - 'Hello and welcome to Native Script. Hope you enjoy the power.'; |
12 | | - this._SpeechSynthesizer = new TNSTextToSpeech() as TNSTextToSpeech; |
13 | | - this._SpeechSynthesizer.speak({ text: '' }); |
14 | | - } |
15 | | - |
16 | | - public speakThis() { |
17 | | - if (!this._SpeechSynthesizer) { |
18 | | - this._SpeechSynthesizer = new TNSTextToSpeech() as TNSTextToSpeech; |
| 5 | + public speakText: string; |
| 6 | + private _SpeechSynthesizer: TNSTextToSpeech; |
| 7 | + |
| 8 | + constructor(mainPage: Page) { |
| 9 | + super(); |
| 10 | + this.speakText = 'Hello and welcome to Native Script. Hope you enjoy the power.'; |
| 11 | + this._SpeechSynthesizer = new TNSTextToSpeech(); |
| 12 | + this._SpeechSynthesizer.init().catch((err) => { |
| 13 | + alert(err); |
| 14 | + }); |
19 | 15 | } |
20 | 16 |
|
21 | | - const opts: SpeakOptions = { |
22 | | - text: this.get('speakText'), |
23 | | - speakRate: isIOS ? 0.45 : null, |
24 | | - finishedCallback: () => { |
25 | | - alert('Finished Speaking :)'); |
26 | | - }, |
27 | | - }; |
28 | | - |
29 | | - this._SpeechSynthesizer.speak(opts); |
30 | | - } |
| 17 | + public async speakThis() { |
| 18 | + if (!this._SpeechSynthesizer) { |
| 19 | + this._SpeechSynthesizer = new TNSTextToSpeech(); |
| 20 | + } |
| 21 | + |
| 22 | + const opts: SpeakOptions = { |
| 23 | + text: this.get('speakText'), |
| 24 | + speakRate: isIOS ? 0.45 : null, |
| 25 | + finishedCallback: () => { |
| 26 | + alert('Finished Speaking :)'); |
| 27 | + }, |
| 28 | + }; |
| 29 | + try { |
| 30 | + await this._SpeechSynthesizer.speak(opts); |
| 31 | + } catch (err) { |
| 32 | + alert(err); |
| 33 | + } |
| 34 | + } |
31 | 35 |
|
32 | | - public byebyeSynthesizer() { |
33 | | - this._SpeechSynthesizer.pause(); |
34 | | - this._SpeechSynthesizer.destroy(); |
35 | | - console.log('destroyed'); |
36 | | - } |
| 36 | + public byebyeSynthesizer() { |
| 37 | + this._SpeechSynthesizer.pause(); |
| 38 | + this._SpeechSynthesizer.destroy(); |
| 39 | + console.log('destroyed'); |
| 40 | + } |
37 | 41 |
|
38 | | - public pauseSynthesizer() { |
39 | | - this._SpeechSynthesizer.pause(); |
40 | | - console.log('paused speech'); |
41 | | - } |
| 42 | + public pauseSynthesizer() { |
| 43 | + this._SpeechSynthesizer.pause(); |
| 44 | + console.log('paused speech'); |
| 45 | + } |
42 | 46 |
|
43 | | - public resumeSynthesizer() { |
44 | | - this._SpeechSynthesizer.resume(); |
45 | | - console.log('resumed speech'); |
46 | | - } |
| 47 | + public resumeSynthesizer() { |
| 48 | + this._SpeechSynthesizer.resume(); |
| 49 | + console.log('resumed speech'); |
| 50 | + } |
47 | 51 | } |
0 commit comments