capacitor-community/text-to-speech

[Android] Unable to get supported voices

Vall98 opened this issue · 1 comments

Describe the bug
As I call TextToSpeech.getSupportedVoices(), the following error appears:
'Error: Cannot find a differ supporting object '[Voice[Name: es-us-x-sfb-local, locale: es_US, quality: 400, latency: 200, requiresNetwork: false, features: [networkTimeoutMs, notInstalled, networkRetriesCount]], Voice[Name: ...'

To Reproduce
Steps to reproduce the behavior:

  1. Import TextToSpeech plugin
  2. Call TextToSpeech.getSupportedVoices()
  3. See error

Expected behavior
return a promise on this object: {voices: SpeechSynthesisVoice[];}

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: Win10
  • Browser Chrome
  • Version [e.g. 22]

Smartphone (please complete the following information):

  • Device: [e.g. iPhone6]
  • OS: Android
  • Browser [e.g. stock browser, safari]
  • Version [e.g. 22]

Additional context

I changed the function "getSupportedVoices" in node_modules@capacitor-community\text-to-speech\android\src\main\java\com\getcapacitor\community\tts\TextToSpeech.java a bit and managed to get it to work. The error itself was caused by a *ngFor loop over the voices, because of a malformation of the data sent back from the java part of the plugin (I guess ?).

Here is my function:

@PluginMethod
  public void getSupportedVoices(PluginCall call) {
    try {
      Set<Voice> voices = tts.getVoices();
      JSArray voicesArray = new JSArray();
      for (Voice v : voices) {
        JSObject jsobj = new JSObject();
        jsobj.put("voiceURI", "");
        jsobj.put("name", v.getName());
        jsobj.put("lang", v.getLocale().getDisplayLanguage() + '-' + v.getLocale().getDisplayCountry());
        jsobj.put("localService", false);
        jsobj.put("default", false);
        voicesArray.put(jsobj);
      }
      call.success(new JSObject().put("voices", voicesArray));
    } catch (Exception ex) {
      call.error(ex.getLocalizedMessage());
    }
  }

Note that I have no idea if it's a good way to solve my issue or not.