Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getSupportedVoices() #112

Closed
mar10droid3v opened this issue Mar 8, 2024 · 8 comments · Fixed by #114
Closed

getSupportedVoices() #112

mar10droid3v opened this issue Mar 8, 2024 · 8 comments · Fixed by #114

Comments

@mar10droid3v
Copy link

Plugin version:
@capacitor-community/text-to-speech": "^3.0.1

Platform(s):
android 12

Current behavior:
Hi when testing on a real device TextToSpeech.getSupportedVoices() is returning error, "Comparison method violates its general contract!"

Any ideas?

@migmax48
Copy link

migmax48 commented Apr 9, 2024

same problem on speak method.

@mar10droid3v
Copy link
Author

I have a working fix but am not a contributor and i dont know how to do this or publish/suggest a fix etc... or step on anyones toes! Its because you cant or shouldn't compare by using a hashcode. For now i just edit the TextToSpeech.java file locally by changing the getSupportedVoices method/function see below... if someone can get it to work and update the plugin would be great... my 2 cents and thanks! :)

public ArrayList<Voice> getSupportedVoicesOrdered() {
    Set<Voice> supportedVoices = tts.getVoices();
    ArrayList<Voice> orderedVoices = new ArrayList<Voice>();
    for (Voice supportedVoice : supportedVoices) {
        orderedVoices.add(supportedVoice);
    }

   /* Collections.sort(
        orderedVoices,
        new Comparator<Voice>() {
            public int compare(Voice v1, Voice v2) {
                return v1.hashCode() - v2.hashCode();
            }
        }
    ); */
    //sort by name instead...
    Collections.sort(
        orderedVoices,
        new Comparator<Voice>() {
          public int compare(Voice v1, Voice v2) {
            return v1.getName().compareTo(v2.getName());
          }
        }
      );


    return orderedVoices;
}

public JSArray getSupportedVoices() {
    ArrayList<JSObject> voices = new ArrayList<>();
    ArrayList<Voice> supportedVoices = getSupportedVoicesOrdered();
    for (Voice supportedVoice : supportedVoices) {
        JSObject obj = this.convertVoiceToJSObject(supportedVoice);
        voices.add(obj);
    }
    JSArray result = JSArray.from(voices.toArray());
    return result;
}

@robingenz
Copy link
Member

PRs are welcome!

@ecc521
Copy link
Contributor

ecc521 commented Apr 21, 2024

@mar10droid3v
Pretty sure I'm the one that wrote this one. Apologies for the bug here.
Should be pretty easy to send in a PR. If you don't send in a PR, I'll probably write one in around a month when I get around to it.

A little surprised to see an issue here - hashCode should be constant for any given object, so transitivity should be preserved. Have you logged the hashCodes and taken a look at what is going on there?

A consistent sorting mechanism is needed, but the mechanism itself is irrelevant, so name should be totally fine.

As for now, if you don't use the voice selection everything will work just fine.

@ecc521
Copy link
Contributor

ecc521 commented Apr 22, 2024

@mar10droid3v @migmax48
What devices are you using? I'm unable to reproduce this issue.

@mar10droid3v
Copy link
Author

@ecc521 hey tucker. happens on xiaomi devices. using name fixed it.

@migmax48
Copy link

@ecc521 Hi Tucker.
I confirm that the problem occurred on Xiaomi devices.
By moving the code before the angular class definition the problem was solved.

@hpflatorre
Copy link
Contributor

@ecc521 Same issue here on a Samsung SM-X210 tablet. The plugin works after the tablet is factory reset. The issue starts after changing the default TTS from Samsung Voice to Google Voice on System settings. Changing it back to Samsung TTS stops the error.

Sending plugin error: {"save":false,"callbackId":"85276539","pluginId":"TextToSpeech","methodName":"getSupportedVoices","success":false,"error":{"message":"Comparison method violates its general contract!"}}

I've also tested @mar10droid3v solution and it works well.

hpflatorre pushed a commit to hpflatorre/text-to-speech that referenced this issue Apr 29, 2024
Fixes capacitor-community#112

Changes compare logic to avoid the issue described without changing the ordering behavior.
robingenz pushed a commit that referenced this issue Apr 29, 2024
…114)

* fix(android) Error: Comparison method violates its general contract!

Fixes a runtime crash on  getSupportedVoices observed on Android after changing the default TTS from Samsung to Google from system settings.

Stack:
@capacitor-community/text-to-speech: 4.0.0,
@capacitor/android : 6.0.0
@ionic/angular:7.8.6

Traces:

Sending plugin error: {"save":false,"callbackId":"72380935","pluginId":"TextToSpeech","methodName":"getSupportedVoices","success":false,"error":{"message":"Comparison method violates its general contract!"}}

* fix(android) Error: Comparison method violates its general contract! revisited

This change to the solution keeps the behavior 100% consistent with original code that orders based on hash instead of name

Fixes a runtime crash on  getSupportedVoices observed on Android after changing the default TTS from Samsung to Google from system settings.

Stack:
@capacitor-community/text-to-speech: 4.0.0,
@capacitor/android : 6.0.0
@ionic/angular:7.8.6

Traces:

Sending plugin error: {"save":false,"callbackId":"72380935","pluginId":"TextToSpeech","methodName":"getSupportedVoices","success":false,"error":{"message":"Comparison method violates its general contract!"}}

* fix(android) Error: Comparison method violates its general contract!

Fixes #112

Changes compare logic to avoid the issue described without changing the ordering behavior.

---------

Co-authored-by: Henrique Latorre <[email protected]>
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 26, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants