• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by r2bougue · May 04, 2017 at 10:57 AM · androidpluginthreadsmain thread

Executing function on main thread from Android Plugin

Hi,

I'm actually developping a mobile app, which uses vocal recognition. To do this, i made an Android Library and imported it. Unfortunately, i can't use my vocal recognition functions because i have an error saying to me that:

 I/Unity: AndroidJavaException: java.lang.RuntimeException: SpeechRecognizer should be used only from the application's main thread

I've tried a lot of things and here is what i did:

The script from where i launch my java object:

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Video; using UnityEngine.UI; using Newtonsoft.Json.Linq; using Vuforia;

public class TargetScript : MonoBehaviour, ITrackableEventHandler {

 //Attributs du script
 public AndroidJavaObject jo;
 public AndroidJavaObject jo2;
 public AndroidJavaObject voiceReco;
 public AndroidJavaClass androidJC;


 void Start()
 {
     init();

 }

 public void init()
 {
     this.androidJC = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
     this.jo = androidJC.GetStatic<AndroidJavaObject>("currentActivity");
     this.voiceReco = new AndroidJavaObject("com.example.r_boug.myunitylib.VoiceRecognition", this.jo);
     
 }


 void Update()
 {
    
 }

  

Here is my Android Plugin Java class used: (VoiceRecognition):

   package com.example.r_boug.myunitylib;
     
     import android.content.Context;
     import android.os.Handler;
     import android.speech.SpeechRecognizer;
     import android.util.Log;
     
     import com.vuzix.speech.Constants;
     import com.vuzix.speech.VoiceControl;
     
     /**
      * Created by r_boug on 02/05/2017.
      */
     
     public class VoiceRecognition  {
     
     
         private VoiceControl vc;
         public VoiceRecognition(Context context) {
          // super(context);
     //        super(context);
             Log.d("AFFICHAGE DU RESULTAT","FONCTION 1");
             // Get a handler that can be used to post to the main thread
     
             SpeechRecognizerThread speechRecognizer=new SpeechRecognizerThread(context);
             speechRecognizer.run();
     
             }
     
         }
 
 
 And finally here is the definition of the thread in the Android Plugin :
 
 package com.example.r_boug.myunitylib;
 
 import android.content.Context;
 import android.util.Log;
 
 import com.vuzix.speech.Constants;
 import com.vuzix.speech.VoiceControl;
 
 /**
  * Created by r_boug on 02/05/2017.
  */
 
 public class SpeechRecognizerThread implements Runnable {
 
     public Context c;
     public VoiceControl vc;
 
     public SpeechRecognizerThread(Context parameter) {
         // store parameter for later user
         c=parameter;
     }
 
     public void run() {
         vc=new VoiceControl(c) {
             @Override
             protected void onRecognition(String s) {
                 Log.d("RECONNAISSAINCE",s);
             }
         };        vc.addGrammar(Constants.GRAMMAR_NAVIGATION);
         Log.d("AFFICHAGE DU RESULTAT","FONCTION 2");
         vc.addGrammar(Constants.GRAMMAR_NAVIGATION);
         Log.d("AFFICHAGE DU RESULTAT","FONCTION 3");
         vc.on();
         Log.d("AFFICHAGE DU RESULTAT","FONCTION 4");
 
 
     }
 }


As you can see, i've tried to call all the plugin functions in the context i passed from Unity. (Maybe isn't it the main application thread?). I know that you'll have ideas to help me !

Thanks,

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by liortal · May 07, 2017 at 05:25 AM

Check out this example of using AndroidJavaRunnable to achieve what you want: https://docs.unity3d.com/ScriptReference/AndroidJavaRunnable.html

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Bad performance on quad core Android phones 0 Answers

Where did the option for Assets -> Plugin -> Android go? 1 Answer

How to show SurfaceView on top of UnityPlayer? (Android plugin) 0 Answers

Android background app. (idea) 0 Answers

Are apps with unity ads allowed in amazon app store?? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges