@@ -36,14 +36,11 @@ class FlutterTtsTizenPlugin : public flutter::Plugin {
36
36
});
37
37
38
38
tts_ = std::make_unique<TextToSpeech>();
39
- try {
40
- tts_->Initialize ();
41
- } catch (const TextToSpeechError &error) {
39
+ if (!tts_->Initialize ()) {
42
40
// TODO : Handle initialization failure cases
43
41
// Rarely, initializing TextToSpeech can fail. we should consider catching
44
42
// the exception and propagating it to the flutter side. however, I think
45
43
// this is optional because flutter side is not expecting any errors.
46
- LOG_ERROR (" Operation failed : %s" , error.GetErrorString ().c_str ());
47
44
tts_ = nullptr ;
48
45
return ;
49
46
}
@@ -89,6 +86,13 @@ class FlutterTtsTizenPlugin : public flutter::Plugin {
89
86
void HandleMethodCall (
90
87
const flutter::MethodCall<flutter::EncodableValue> &method_call,
91
88
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
89
+ // Keep in sync with the return values implemented in:
90
+ // https://github.com/dlutton/flutter_tts/blob/master/android/src/main/java/com/tundralabs/fluttertts/FlutterTtsPlugin.java.
91
+ // In principle, MethodResult was designed to call MethodResult.Error() to
92
+ // notify the dart code of any method call failures from the host platform.
93
+ // However, in the case of flutter_tts, it expects a return value 0 on
94
+ // failure(and value 1 on success). Therefore in the scope of this plugin,
95
+ // we call SendResult(flutter::EncodableValue(0)); to notify errors.
92
96
const auto method_name = method_call.method_name ();
93
97
const auto &arguments = *method_call.arguments ();
94
98
@@ -128,62 +132,58 @@ class FlutterTtsTizenPlugin : public flutter::Plugin {
128
132
SendResult (flutter::EncodableValue (1 ));
129
133
return ;
130
134
}
131
- SendErrorResult ( " Invalid argument " , " Argument is invaild. " );
135
+ SendResult ( flutter::EncodableValue ( 0 ) );
132
136
}
133
137
134
138
void OnSpeak (const flutter::EncodableValue &arguments) {
135
- try {
136
- if (tts_->GetState () == TtsState::kPlaying ) {
137
- SendErrorResult (" Operation cancelled" ,
138
- " You cannot speak again while speaking." );
139
- return ;
139
+ TtsState state = tts_->GetState ();
140
+ if (state == TtsState::kPlaying || state == TtsState::kError ) {
141
+ if (state == TtsState::kPlaying ) {
142
+ LOG_ERROR (" [TTS] : You cannot speak again while speaking." );
140
143
}
144
+ SendResult (flutter::EncodableValue (0 ));
145
+ return ;
146
+ }
141
147
142
- if (std::holds_alternative<std::string>(arguments)) {
143
- std::string text = std::get<std::string>(arguments);
144
- tts_->AddText (text);
148
+ if (std::holds_alternative<std::string>(arguments)) {
149
+ std::string text = std::get<std::string>(arguments);
150
+ if (!tts_->AddText (text)) {
151
+ SendResult (flutter::EncodableValue (0 ));
152
+ return ;
145
153
}
146
-
147
- tts_->Speak ();
148
-
149
- } catch (const TextToSpeechError &error) {
150
- SendErrorResult (" Operation failed" , error.GetErrorString ());
151
154
}
152
155
153
- if (await_speak_completion_ && !result_for_await_speak_completion_) {
154
- LOG_DEBUG (" Store result ptr for await speak completion" );
155
- result_for_await_speak_completion_ = std::move (result_);
156
+ if (tts_->Speak ()) {
157
+ if (await_speak_completion_ && !result_for_await_speak_completion_) {
158
+ LOG_DEBUG (" Store result ptr for await speak completion" );
159
+ result_for_await_speak_completion_ = std::move (result_);
160
+ } else {
161
+ SendResult (flutter::EncodableValue (1 ));
162
+ }
156
163
} else {
157
- SendResult (flutter::EncodableValue (1 ));
164
+ SendResult (flutter::EncodableValue (0 ));
158
165
}
159
166
}
160
167
161
168
void OnStop () {
162
- try {
163
- tts_-> Stop ( );
164
- } catch ( const TextToSpeechError &error) {
165
- SendErrorResult ( " Operation failed " , error. GetErrorString ( ));
169
+ if (tts_-> Stop ()) {
170
+ SendResult ( flutter::EncodableValue ( 1 ) );
171
+ } else {
172
+ SendResult ( flutter::EncodableValue ( 0 ));
166
173
}
167
- SendResult (flutter::EncodableValue (1 ));
168
174
}
169
175
170
176
void OnPause () {
171
- try {
172
- tts_-> Pause ( );
173
- } catch ( const TextToSpeechError &error) {
174
- SendErrorResult ( " Operation failed " , error. GetErrorString ( ));
177
+ if (tts_-> Pause ()) {
178
+ SendResult ( flutter::EncodableValue ( 1 ) );
179
+ } else {
180
+ SendResult ( flutter::EncodableValue ( 0 ));
175
181
}
176
- SendResult (flutter::EncodableValue (1 ));
177
182
}
178
183
179
184
void OnGetSpeechRateValidRange () {
180
185
int min = 0 , normal = 0 , max = 0 ;
181
- try {
182
- tts_->GetSpeedRange (&min, &normal , &max);
183
- } catch (const TextToSpeechError &error) {
184
- SendErrorResult (" Operation failed" , error.GetErrorString ());
185
- return ;
186
- }
186
+ tts_->GetSpeedRange (&min, &normal , &max);
187
187
flutter::EncodableMap map;
188
188
map.insert (std::pair<flutter::EncodableValue, flutter::EncodableValue>(
189
189
" min" , min));
@@ -203,7 +203,7 @@ class FlutterTtsTizenPlugin : public flutter::Plugin {
203
203
SendResult (flutter::EncodableValue (1 ));
204
204
return ;
205
205
}
206
- SendErrorResult ( " Invalid argument " , " SpeechRate is invaild. " );
206
+ SendResult ( flutter::EncodableValue ( 0 ) );
207
207
}
208
208
209
209
void OnSetLanguage (const flutter::EncodableValue &arguments) {
@@ -213,7 +213,7 @@ class FlutterTtsTizenPlugin : public flutter::Plugin {
213
213
SendResult (flutter::EncodableValue (1 ));
214
214
return ;
215
215
}
216
- SendErrorResult ( " Invalid argument " , " Language is invaild. " );
216
+ SendResult ( flutter::EncodableValue ( 0 ) );
217
217
}
218
218
219
219
void OnGetLanguage () {
@@ -226,15 +226,13 @@ class FlutterTtsTizenPlugin : public flutter::Plugin {
226
226
227
227
void OnSetVolume (const flutter::EncodableValue &arguments) {
228
228
if (std::holds_alternative<double >(arguments)) {
229
- double volume = std::get<double >(arguments);
230
- try {
231
- tts_->SetVolume (volume);
232
- } catch (const TextToSpeechError &error) {
233
- SendErrorResult (" Operation failed" , error.GetErrorString ());
229
+ double rate = std::get<double >(arguments);
230
+ if (tts_->SetVolume (rate)) {
231
+ SendResult (flutter::EncodableValue (1 ));
234
232
return ;
235
233
}
236
234
}
237
- SendErrorResult ( " Invalid argument " , " Volume is invaild. " );
235
+ SendResult ( flutter::EncodableValue ( 0 ) );
238
236
}
239
237
240
238
void SendResult (const flutter::EncodableValue &result) {
@@ -245,23 +243,6 @@ class FlutterTtsTizenPlugin : public flutter::Plugin {
245
243
result_ = nullptr ;
246
244
}
247
245
248
- void SendErrorResult (const std::string &error_code,
249
- const std::string &error_message) {
250
- // Keep in sync with the return values implemented in:
251
- // https://github.com/dlutton/flutter_tts/blob/master/android/src/main/java/com/tundralabs/fluttertts/FlutterTtsPlugin.java.
252
- // In principle, MethodResult was designed to call MethodResult.Error() to
253
- // notify the dart code of any method call failures from the host platform.
254
- // However, in the case of flutter_tts, it expects a return value 0 on
255
- // failure(and value 1 on success). Therefore in the scope of this plugin,
256
- // we call result_->Success(flutter::EncodableValue(0)) to notify errors.
257
- if (!result_) {
258
- return ;
259
- }
260
- LOG_ERROR (" %s" , std::string (error_code + " : " + error_message).c_str ());
261
- result_->Success (flutter::EncodableValue (0 ));
262
- result_ = nullptr ;
263
- }
264
-
265
246
void HandleAwaitSpeakCompletion (int value) {
266
247
if (await_speak_completion_) {
267
248
LOG_DEBUG (" Send result for await speak completion[%d]" , value);
0 commit comments