@@ -13,52 +13,116 @@ namespace CognitiveSpeechService
13
13
public partial class MainPage : ContentPage
14
14
{
15
15
SpeechRecognizer recognizer ;
16
- IMicrophoneService microphoneService ;
16
+ IMicrophoneService micService ;
17
+ bool isTranscribing = false ;
17
18
18
19
public MainPage ( )
19
20
{
20
21
InitializeComponent ( ) ;
21
22
22
- microphoneService = DependencyService . Resolve < IMicrophoneService > ( ) ;
23
+ micService = DependencyService . Resolve < IMicrophoneService > ( ) ;
23
24
}
24
25
25
- async void EnableMicClicked ( object sender , EventArgs e )
26
+ async void TranscribeClicked ( object sender , EventArgs e )
26
27
{
27
- bool isMicEnabled = await microphoneService . GetPermissionAsync ( ) ;
28
- if ( ! isMicEnabled )
28
+ bool isMicEnabled = await micService . GetPermissionAsync ( ) ;
29
+
30
+ // Speech recognizer
31
+ if ( recognizer == null )
29
32
{
30
- UpdateTranscription ( "Please grant access to the microphone!" , true ) ;
33
+ var config = SpeechConfig . FromSubscription ( Constants . CognitiveServicesApiKey , Constants . CognitiveServicesRegion ) ;
34
+ recognizer = new SpeechRecognizer ( config ) ;
35
+ recognizer . Recognized += ( obj , args ) =>
36
+ {
37
+ UpdateTranscription ( args . Result . Text ) ;
38
+ } ;
31
39
}
32
- }
33
40
34
- void StartTranscribingClicked ( object sender , EventArgs e )
35
- {
36
- var config = SpeechConfig . FromSubscription ( Constants . CognitiveServicesApiKey , Constants . CognitiveServicesRegion ) ;
41
+ // EARLY OUT: make sure mic is accessible
42
+ if ( ! isMicEnabled )
43
+ {
44
+ UpdateTranscription ( "Please grant access to the microphone!" ) ;
45
+ return ;
46
+ }
47
+
48
+ // if already transcribing, stop speech recognizer
49
+ if ( isTranscribing )
50
+ {
51
+ Device . BeginInvokeOnMainThread ( ( ) =>
52
+ {
53
+ transcribingIndicator . IsRunning = false ;
54
+ } ) ;
37
55
38
- UpdateTranscription ( "" , true ) ;
56
+ try
57
+ {
58
+ await recognizer . StopContinuousRecognitionAsync ( ) ;
59
+ }
60
+ catch ( Exception ex )
61
+ {
62
+ UpdateTranscription ( ex . Message ) ;
63
+ }
64
+
65
+ isTranscribing = false ;
66
+ }
39
67
40
- recognizer = new SpeechRecognizer ( config ) ;
41
- recognizer . StartContinuousRecognitionAsync ( ) ;
42
- recognizer . Recognized += ( obj , args ) =>
68
+ // if not transcribing, clear existing text and start speech recognizer
69
+ else
43
70
{
44
- UpdateTranscription ( args . Result . Text ) ;
45
- } ;
71
+ Device . BeginInvokeOnMainThread ( ( ) =>
72
+ {
73
+ transcribingIndicator . IsRunning = true ;
74
+ InsertDateTimeRecord ( ) ;
75
+ } ) ;
76
+
77
+ try
78
+ {
79
+ await recognizer . StartContinuousRecognitionAsync ( ) ;
80
+ }
81
+ catch ( Exception ex )
82
+ {
83
+ UpdateTranscription ( ex . Message ) ;
84
+ }
85
+
86
+ isTranscribing = true ;
87
+ }
88
+
89
+ UpdateButton ( ) ;
46
90
}
47
91
48
- void UpdateTranscription ( string newText , bool clearText = false )
92
+ void UpdateTranscription ( string newText )
49
93
{
50
- // Force on main thread since this is likely called
51
- // from a background event
52
94
Device . BeginInvokeOnMainThread ( ( ) =>
53
95
{
54
- if ( clearText )
96
+ if ( ! string . IsNullOrWhiteSpace ( newText ) )
55
97
{
56
- transcribedText . Text = "" ;
98
+ transcribedText . Text += $ "{ newText } \n ";
99
+ scroll . ScrollToAsync ( 0 , scroll . Height , true ) ;
57
100
}
58
-
59
- transcribedText . Text += newText ;
60
101
} ) ;
61
102
62
103
}
104
+
105
+ void InsertDateTimeRecord ( )
106
+ {
107
+ var msg = $ "=================\n { DateTime . Now . ToString ( ) } \n =================";
108
+ UpdateTranscription ( msg ) ;
109
+ }
110
+
111
+ void UpdateButton ( )
112
+ {
113
+ Device . BeginInvokeOnMainThread ( ( ) =>
114
+ {
115
+ if ( isTranscribing )
116
+ {
117
+ transcribeButton . Text = "Stop" ;
118
+ transcribeButton . BackgroundColor = Color . Red ;
119
+ }
120
+ else
121
+ {
122
+ transcribeButton . Text = "Transcribe" ;
123
+ transcribeButton . BackgroundColor = Color . Green ;
124
+ }
125
+ } ) ;
126
+ }
63
127
}
64
128
}
0 commit comments