Skip to content

Commit

Permalink
fix(android): get supported voices (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
robingenz authored Mar 8, 2021
1 parent bf477aa commit 0870389
Showing 1 changed file with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,15 @@ public void openInstall(PluginCall call) {
@PluginMethod
public void getSupportedVoices(PluginCall call) {
try {
Set<Voice> voices = tts.getVoices();
call.success(new JSObject().put("voices", voices));
ArrayList<JSObject> voices = new ArrayList<>();
Set<Voice> supportedVoices = tts.getVoices();
for (Voice supportedVoice : supportedVoices) {
JSObject obj = this.convertVoiceToJSObject(supportedVoice);
voices.add(obj);
}
JSObject ret = new JSObject();
ret.put("voices", JSArray.from(voices.toArray()));
call.success(ret);
} catch (Exception ex) {
call.error(ex.getLocalizedMessage());
}
Expand All @@ -262,4 +269,15 @@ public void getSupportedVoices(PluginCall call) {
private boolean isStringValid(String value) {
return (value != null && !value.isEmpty() && !value.equals("null"));
}

private JSObject convertVoiceToJSObject(Voice voice) {
Locale locale = voice.getLocale();
JSObject obj = new JSObject();
obj.put("voiceURI", voice.getName());
obj.put("name", locale.getDisplayLanguage() + " " + locale.getDisplayCountry());
obj.put("lang", locale.toLanguageTag());
obj.put("localService", !voice.isNetworkConnectionRequired());
obj.put("default", false);
return obj;
}
}

0 comments on commit 0870389

Please sign in to comment.