@@ -61,10 +61,6 @@ internal class TextToSpeechImplementation : Java.Lang.Object, AndroidTextToSpeec
61
61
TaskCompletionSource < bool > tcsInitialize ;
62
62
TaskCompletionSource < bool > tcsUtterances ;
63
63
64
- public TextToSpeechImplementation ( )
65
- {
66
- }
67
-
68
64
Task < bool > Initialize ( )
69
65
{
70
66
if ( tcsInitialize != null && tts != null )
@@ -88,11 +84,16 @@ Task<bool> Initialize()
88
84
return tcsInitialize . Task ;
89
85
}
90
86
91
- public new void Dispose ( )
87
+ protected override void Dispose ( bool disposing )
92
88
{
93
- tts ? . Stop ( ) ;
94
- tts ? . Shutdown ( ) ;
95
- tts = null ;
89
+ if ( disposing )
90
+ {
91
+ tts ? . Stop ( ) ;
92
+ tts ? . Shutdown ( ) ;
93
+ tts = null ;
94
+ }
95
+
96
+ base . Dispose ( disposing ) ;
96
97
}
97
98
98
99
int numExpectedUtterances = 0 ;
@@ -138,10 +139,9 @@ public async Task SpeakAsync(string text, int max, SpeakSettings settings, Cance
138
139
}
139
140
140
141
if ( settings ? . Pitch . HasValue ?? false )
141
- {
142
- var pitch = settings . Pitch . Value ;
143
- tts . SetPitch ( pitch ) ;
144
- }
142
+ tts . SetPitch ( settings . Pitch . Value ) ;
143
+ else
144
+ tts . SetPitch ( TextToSpeech . PitchDefault ) ;
145
145
146
146
var parts = text . SplitSpeak ( max ) ;
147
147
@@ -150,11 +150,8 @@ public async Task SpeakAsync(string text, int max, SpeakSettings settings, Cance
150
150
151
151
var guid = Guid . NewGuid ( ) . ToString ( ) ;
152
152
153
- for ( var i = 0 ; i < parts . Count ; i ++ )
153
+ for ( var i = 0 ; i < parts . Count && ! cancelToken . IsCancellationRequested ; i ++ )
154
154
{
155
- if ( cancelToken . IsCancellationRequested )
156
- break ;
157
-
158
155
// We require the utterance id to be set if we want the completed listener to fire
159
156
var map = new Dictionary < string , string >
160
157
{
@@ -164,10 +161,9 @@ public async Task SpeakAsync(string text, int max, SpeakSettings settings, Cance
164
161
if ( settings != null && settings . Volume . HasValue )
165
162
map . Add ( AndroidTextToSpeech . Engine . KeyParamVolume , settings . Volume . Value . ToString ( ) ) ;
166
163
167
- #pragma warning disable CS0618
168
-
169
164
// We use an obsolete overload here so it works on older API levels at runtime
170
165
// Flush on first entry and add (to not flush our own previous) subsequent entries
166
+ #pragma warning disable CS0618
171
167
tts . Speak ( parts [ i ] , i == 0 ? QueueMode . Flush : QueueMode . Add , map ) ;
172
168
#pragma warning restore CS0618
173
169
}
@@ -178,13 +174,9 @@ public async Task SpeakAsync(string text, int max, SpeakSettings settings, Cance
178
174
public void OnInit ( OperationResult status )
179
175
{
180
176
if ( status . Equals ( OperationResult . Success ) )
181
- {
182
177
tcsInitialize . TrySetResult ( true ) ;
183
- }
184
178
else
185
- {
186
179
tcsInitialize . TrySetException ( new ArgumentException ( "Failed to initialize Text to Speech engine." ) ) ;
187
- }
188
180
}
189
181
190
182
public async Task < IEnumerable < Locale > > GetLocalesAsync ( )
@@ -203,27 +195,30 @@ public async Task<IEnumerable<Locale>> GetLocalesAsync()
203
195
}
204
196
}
205
197
206
- return JavaLocale . GetAvailableLocales ( ) .
207
- Where ( l =>
208
- {
209
- try
210
- {
211
- var r = tts . IsLanguageAvailable ( l ) ;
212
- return r == LanguageAvailableResult . Available
213
- || r == LanguageAvailableResult . CountryAvailable
214
- || r == LanguageAvailableResult . CountryVarAvailable ;
215
- }
216
- catch ( Exception ex )
217
- {
218
- Console . WriteLine ( "Error checking language; " + l + " " + ex ) ;
219
- }
220
- return false ;
221
- } ) .
222
- Select ( l => new Locale ( l . Language , l . Country , l . DisplayName , string . Empty ) )
198
+ return JavaLocale . GetAvailableLocales ( )
199
+ . Where ( IsLocaleAvailable )
200
+ . Select ( l => new Locale ( l . Language , l . Country , l . DisplayName , string . Empty ) )
223
201
. GroupBy ( c => c . ToString ( ) )
224
202
. Select ( g => g . First ( ) ) ;
225
203
}
226
204
205
+ private bool IsLocaleAvailable ( JavaLocale l )
206
+ {
207
+ try
208
+ {
209
+ var r = tts . IsLanguageAvailable ( l ) ;
210
+ return
211
+ r == LanguageAvailableResult . Available ||
212
+ r == LanguageAvailableResult . CountryAvailable ||
213
+ r == LanguageAvailableResult . CountryVarAvailable ;
214
+ }
215
+ catch ( Exception ex )
216
+ {
217
+ Console . WriteLine ( "Error checking language; " + l + " " + ex ) ;
218
+ }
219
+ return false ;
220
+ }
221
+
227
222
public void OnUtteranceCompleted ( string utteranceId )
228
223
{
229
224
numCompletedUtterances ++ ;
0 commit comments