khanhuitse05 / speech-and-text-unity-ios-android

Speed to text in Unity iOS use Native Speech Recognition
MIT License
286 stars 124 forks source link

Error on Samsung S8 #73

Open PersistantStudio opened 2 years ago

PersistantStudio commented 2 years ago

Hi, recently it seems that the plugin encounter difficulties to run on some devices, on android 8/9 , ( samsung S8), while still working correctly on a Pixel 6.

On the S8 I receive a lot of RMS Changed callbacks ( that seems to be related to the sound level) and then the application crash with an IndexOutOfBoundException in the onPartialResults Handler. For some reason it seems that the StringArrayList is empty, leading to the instruction text.get(0) to crash.

I'm mostly suprised by what I'm interpreting to be a sudden incompatibility, as the application was properly working precedently.

Even weirder, by trying to create the minimal project to try to reduce the prism, I narrow the condition of reproduction to be directly related by the change in the the custom manifest files as I only experience this crash when I deactivate the show popup option, as the method StartRecording access the MainActivity. The Vocal recognition seems to be working with no custom manifest and the option Show Popup set as true.

kayabilgehan commented 2 years ago

We have same problem. Can you share the solution with us, if you fixed the issue?

hojjatabdollahi commented 2 years ago

We are having the same issue. The app crashes if you try to hide the pop up. But it works fine if you show the pop up (which covers the whole app).

Did anybody come up with any solutions to this?

PersistantStudio commented 2 years ago

We were able to fix the problem, wich is pretty simple to adress Just adding null checks ans size check before accessing the entry 0 in the array in OnResults & OnPartialResults.

   public void onResults(Bundle results) {
            ArrayList<String> text = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
            if(text != null && text.size() >0) {
            UnityPlayer.UnitySendMessage("SpeechToText", "onResults", text.get(0));
            }
        }
        @Override
        public void onPartialResults(Bundle partialResults) {
            ArrayList<String> text = partialResults.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
            if(text != null && text.size() >0) {
            UnityPlayer.UnitySendMessage("SpeechToText", "onPartialResults", text.get(0));
            }
        }`

The hardest part was in fact to build the plugin thas is really outdated. It's way easier to start from scratch in fact.

hojjatabdollahi commented 2 years ago

@PersistantStudio , Thank you.

It took us a whole day to get android studio to compile this. We had already added a check on text.size() but adding the check for null fixed the crash. That being said, our app is now stuck, which I assume is because if the if is not true (the text is empty) nothing is sent to Unity. We are working on fixing this by sending an empty string. Maybe that works!!

Thanks again.

PersistantStudio commented 2 years ago

I think we encountered the same issue.The call are made by name on gameobject on the Java side, so the name in Unity3D need to match the one defined in the Java plugin. The code on this repo contains an error in the way the game object is named in Unity : It is actually named TextToSpeech instead of SpeechToText when you use the lazy instanciation. Correcting this error will fix the issue of not getting any callbacks.

kayabilgehan commented 2 years ago

We fixed the issue by storing the text created in "onPartialResults" in a variable and if the data comes from "onResults" is null, than we use the stored text data. Code:

private Intent intent;

String partialStringResult = ""; //variable to store text from onPartialResults

@Override public void onCreate(Bundle savedInstanceState) {

. . .

@Override public void onResults(Bundle results) { ArrayList text = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); if(text != null && text.size() > 0) { UnityPlayer.UnitySendMessage("SpeechToText", "onResults", text.get(0)); } else if(partialStringResult != null && !partialStringResult.equals("")) { UnityPlayer.UnitySendMessage("SpeechToText", "onResults", partialStringResult); //send unity the stored string } } @Override public void onPartialResults(Bundle partialResults) { ArrayList text = partialResults.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); if(text != null && text.size() > 0) { partialStringResult = text.get(0); //store data to use it if needed UnityPlayer.UnitySendMessage("SpeechToText", "onPartialResults", text.get(0)); } }

Edit: After these changes in Java I made changes below too in build.gradle in app level.

task deleteOldJar(type: Delete) { delete 'release/SpeechToTextPlugin.jar' }

//task to export content as jar task exportJar(type: Copy) { from('build/intermediates/compile_library_classes_jar/release/') into('release/') include('classes.jar') rename('classes.jar', 'SpeechToTextPlugin.jar') }

exportJar.dependsOn(deleteOldJar, build)

medinags commented 2 years ago

@kayabilgehan Thanks for your solution, but my question is where should I implement this solution, in Unity?

PersistantStudio commented 2 years ago

Nope the modifications needs to be made in the java project of the plugin.

kayabilgehan commented 2 years ago

@medinags you have to make this changes in android studio with java like @PersistantStudio said.

takijemai commented 2 years ago

hello, sorry i have the same question, i m new with unity and i do this task, where i can found the java project of the plugin please?

pedropll98 commented 2 years ago

I have the same problem and when I installed Android Studio it told me that I need HAXM to work, but my computer does not meet the requirements. What can I do to edit the plugin?

pedropll98 commented 2 years ago

Can anyone share the plugin already fixed?

Aneeb151 commented 2 years ago

SpeechToTextPlugin.jar.zip Anybody suffering from this problem can first update their files from the repository to latest (specially SpeechToText.cs and TextToSpeech.cs) and then replace their jar file from "Assets/Plugins/Android/SpeechToTextPlugin.jar". I have followed and updated @kayabilgehan solution as it had a type casting error and compiled the files from android studio. Thanks @PersistantStudio and @kayabilgehan for your efforts :)

takijemai commented 1 year ago

SpeechToTextPlugin.jar.zip Anybody suffering from this problem can first update their files from the repository to latest (specially SpeechToText.cs and TextToSpeech.cs) and then replace their jar file from "Assets/Plugins/Android/SpeechToTextPlugin.jar". I have followed and updated @kayabilgehan solution as it had a type casting error and compiled the files from android studio. Thanks @PersistantStudio and @kayabilgehan for your efforts :)

hello ,thank you for the reply but even i change the jar file and update the two files of textspeech and speechtotext it's does'nt work

Sukanta-Patra commented 1 year ago

SpeechToTextPlugin.jar.zip Anybody suffering from this problem can first update their files from the repository to latest (specially SpeechToText.cs and TextToSpeech.cs) and then replace their jar file from "Assets/Plugins/Android/SpeechToTextPlugin.jar". I have followed and updated @kayabilgehan solution as it had a type casting error and compiled the files from android studio. Thanks @PersistantStudio and @kayabilgehan for your efforts :)

This fixed the issue. Thank you so much man! <3

GSJuan commented 1 year ago

SpeechToTextPlugin.jar.zip Anybody suffering from this problem can first update their files from the repository to latest (specially SpeechToText.cs and TextToSpeech.cs) and then replace their jar file from "Assets/Plugins/Android/SpeechToTextPlugin.jar". I have followed and updated @kayabilgehan solution as it had a type casting error and compiled the files from android studio. Thanks @PersistantStudio and @kayabilgehan for your efforts :)

Saved my life @Aneeb151. Thank you very much!

usamaqh commented 1 year ago

SpeechToTextPlugin.jar.zip Anybody suffering from this problem can first update their files from the repository to latest (specially SpeechToText.cs and TextToSpeech.cs) and then replace their jar file from "Assets/Plugins/Android/SpeechToTextPlugin.jar". I have followed and updated @kayabilgehan solution as it had a type casting error and compiled the files from android studio. Thanks @PersistantStudio and @kayabilgehan for your efforts :)

Thanks a lot brother!

Blindsp0t-creative commented 1 year ago

SpeechToTextPlugin.jar.zip Anybody suffering from this problem can first update their files from the repository to latest (specially SpeechToText.cs and TextToSpeech.cs) and then replace their jar file from "Assets/Plugins/Android/SpeechToTextPlugin.jar". I have followed and updated @kayabilgehan solution as it had a type casting error and compiled the files from android studio. Thanks @PersistantStudio and @kayabilgehan for your efforts :)

Thank you so much for this !!