Skip to content

Commit 2650cfb

Browse files
authored
Adds recognize from microphone and stop listening to the application for IOS (#34)
1 parent 4e558a6 commit 2650cfb

File tree

2 files changed

+48
-31
lines changed

2 files changed

+48
-31
lines changed

examples/ionic-angular/src/app/speechToTextTab/speechToText.page.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
class="capturedText"
1818
disabled
1919
readonly
20-
auto-grow="true">
20+
auto-grow="true">
21+
{{capturedText}}
2122
</ion-textarea>
2223
</ion-item>
2324
<ion-button
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,54 @@
1-
import { Component, ViewChild, ElementRef } from '@angular/core';
2-
import { CognitiveServices } from '@ionic-native/cognitiveservices/ngx';
1+
import {Component, ViewChild, ElementRef} from '@angular/core';
2+
import {CognitiveServices} from '@ionic-native/cognitiveservices/ngx';
33

44

55
@Component({
6-
selector: 'app-speechToTextTab',
7-
templateUrl: 'speechToText.page.html',
8-
styleUrls: ['speechToText.page.scss']
6+
selector: 'app-speechToTextTab',
7+
templateUrl: 'speechToText.page.html',
8+
styleUrls: ['speechToText.page.scss']
99
})
1010

1111
export class SpeechToTextPage {
12-
captureButtonText = 'Capture Speech';
13-
captureButtonColor = 'primary';
14-
capturePressed = false;
15-
16-
constructor(private cognitiveServices: CognitiveServices) { }
17-
18-
captureSpeechButtonClicked() {
19-
this.toggleSpeechButton(!this.capturePressed);
20-
21-
}
22-
stopAudioCapture() {
23-
this.toggleSpeechButton(false);
24-
}
25-
26-
toggleSpeechButton(state: boolean) {
27-
if (state) {
28-
this.capturePressed = true;
29-
this.captureButtonText = 'Stop Capture';
30-
this.captureButtonColor = 'danger';
31-
} else {
32-
alert('Audio capture ended');
33-
this.capturePressed = false;
34-
this.captureButtonText = 'Capture Speech';
35-
this.captureButtonColor = 'primary';
12+
captureButtonText = 'Capture Speech';
13+
captureButtonColor = 'primary';
14+
capturePressed = false;
15+
capturedText: string[];
16+
17+
constructor(private cognitiveServices: CognitiveServices) {
18+
}
19+
20+
captureSpeechButtonClicked() {
21+
this.toggleSpeechButton(!this.capturePressed);
22+
23+
}
24+
25+
stopAudioCapture() {
26+
this.toggleSpeechButton(false);
27+
}
28+
29+
toggleSpeechButton(state: boolean) {
30+
if (state) {
31+
this.startListening();
32+
this.capturePressed = true;
33+
this.captureButtonText = 'Stop Capture';
34+
this.captureButtonColor = 'danger';
35+
} else {
36+
this.stopListening();
37+
this.capturePressed = false;
38+
this.captureButtonText = 'Capture Speech';
39+
this.captureButtonColor = 'primary';
40+
}
41+
}
42+
43+
startListening() {
44+
this.cognitiveServices.RecognizeFromMicrophone().subscribe(results => {
45+
this.capturedText = results['result'];
46+
}, (str: any) => {
47+
alert(str);
48+
});
49+
}
50+
51+
stopListening() {
52+
this.cognitiveServices.StopListening();
3653
}
37-
}
3854
}

0 commit comments

Comments
 (0)