EricBatlle / UnityAndroidSpeechRecognizer

🗣️ Speech recognition on Unity and Android without the annoying google popup!
MIT License
60 stars 12 forks source link

error #14

Closed romygt closed 1 year ago

romygt commented 1 year ago

I edited your SpeechRecognizerPlugin to be like below but when I use it in android somtimes it bug and I dkn why

using TMPro;
using UnityEngine;
using UnityEngine.UI;
using static SpeechRecognizerPlugin;

public class SpeechRecognizer : MonoBehaviour, ISpeechRecognizerPlugin
{
    [SerializeField] private Button startListeningBtn = null;
    //[SerializeField] private Button stopListeningBtn = null;
    //  [SerializeField] private Toggle continuousListeningTgle = null;
    //[SerializeField] private TMP_Dropdown languageDropdown = null;
    //[SerializeField] private TMP_InputField maxResultsInputField = null;
    [SerializeField] private Text resultsTxt = null;
    [SerializeField] private Text errorsTxt = null;
    public justBrainandTumor justBrainandTumor;
    public GameObject apear;

    private int clickCount;

    private SpeechRecognizerPlugin plugin = null;

    private void Start()
    {
        plugin = SpeechRecognizerPlugin.GetPlatformPluginVersion(this.gameObject.name);

        startListeningBtn.onClick.AddListener(StartListening);
        // stopListeningBtn.onClick.AddListener(StopListening);
        // continuousListeningTgle.onValueChanged.AddListener(SetContinuousListening);
        // languageDropdown.onValueChanged.AddListener(SetLanguage);
        //maxResultsInputField.onEndEdit.AddListener(SetMaxResults);
    }

    private void StartListening()
    {
        justBrainandTumor = GameObject.Find("justBrainandTumor").GetComponent<justBrainandTumor>();

        // Add keywords to the dictionary with corresponding actions

        clickCount++;
        if (clickCount == 1)
        {
            apear.gameObject.SetActive(true);

            plugin.StartListening();
            plugin.SetContinuousListening(true);

        }
        else
        {
            if (IsEven(clickCount))
            {
                apear.gameObject.SetActive(false);

                //plugin.StopListening();
                StopListening();
            }
            else
            {
                apear.gameObject.SetActive(true);

                plugin.StartListening();
                plugin.SetContinuousListening(true);
            }

        }

    }

    private void StopListening()
    {
        plugin.StopListening();
    }

    private void SetContinuousListening(bool isContinuous)
    {
        plugin.SetContinuousListening(isContinuous);
    }

    private void SetLanguage(int dropdownValue)
    {
        //  string newLanguage = languageDropdown.options[dropdownValue].text;
        //plugin.SetLanguageForNextRecognition(newLanguage);
    }

    private void SetMaxResults(string inputValue)
    {
        if (string.IsNullOrEmpty(inputValue))
            return;

        int maxResults = int.Parse(inputValue);
        plugin.SetMaxResultsForNextRecognition(maxResults);
    }

    public void OnResult(string recognizedResult)
    {
        char[] delimiterChars = { '~' };
        string[] result = recognizedResult.Split(delimiterChars);

        resultsTxt.text = "";
        resultsTxt.text += result[0];
        /* for (int i = 0; i < result.Length; i++)
         {
             resultsTxt.text += result[i] + '\n';
         }*/
        if (resultsTxt.text == "move up")
        {
            MoveUp();
        }
        if (resultsTxt.text == "move left")
        {
            MoveLeft();
        }
        if (resultsTxt.text == "move right")
        {
            MoveRight();
        }
    }

    public void OnError(string recognizedError)
    {
        ERROR error = (ERROR)int.Parse(recognizedError);
        switch (error)
        {
            case ERROR.UNKNOWN:
                Debug.Log("<b>ERROR: </b> Unknown");
                errorsTxt.text += "Unknown";
                break;
            case ERROR.INVALID_LANGUAGE_FORMAT:
                Debug.Log("<b>ERROR: </b> Language format is not valid");
                errorsTxt.text += "Language format is not valid";
                break;
            default:
                break;
        }
    }
    private void Here()
    {
        justBrainandTumor.transform.Translate(0, 1 * Time.deltaTime, 0);

    }
    private void MoveUp()
    {
        justBrainandTumor.transform.Translate(0, 0, 2 * Time.deltaTime);
        Debug.Log("Moving up...");
    }

    private void MoveDown()
    {
        justBrainandTumor.transform.Translate(0, 0, -2 * Time.deltaTime);
        Debug.Log("Moving down...");
    }
    private void MoveLeft()
    {
        justBrainandTumor.transform.Translate(0, 2 * Time.deltaTime, 0);
        Debug.Log("Here up...");
        /* if (Text2 != null)
         {
             PanelInter.transform.position +=new Vector3(0, 2 * Time.deltaTime, 0);
             Text2.transform.position +=new Vector3(0, 2 * Time.deltaTime, 0);
         }*/
        Debug.Log("Moving Left...");
    }
    private void MoveRight()
    {
        justBrainandTumor.transform.Translate(0, -2 * Time.deltaTime, 0);
        Debug.Log("Moving right...");
    }
    private void Rotate()
    {
        justBrainandTumor.transform.Rotate(0, 0, 100f * Time.deltaTime);
        Debug.Log("Moving down...");
    }
    private void RotateLeft()
    {
        justBrainandTumor.transform.Rotate(0, 0, -1000f * Time.deltaTime);
        Debug.Log("Rotate left...");
    }
    private void RotateRight()
    {
        justBrainandTumor.transform.Rotate(0, 0, 1000f * Time.deltaTime);
        Debug.Log("Rotate right...");
    }
    private void RotateUp()
    {
        justBrainandTumor.transform.Rotate(-2000f * Time.deltaTime, 0, 0);
        Debug.Log("Rotate up...");
    }
    private void RotateDown()
    {
        justBrainandTumor.transform.Rotate(2000f * Time.deltaTime, 0, 0);
        Debug.Log("Rotate down...");
    }
    private void ZoomIn()
    {
        justBrainandTumor.transform.localScale += new Vector3(0.05f, 0.05f, 0.05f);
        Debug.Log("Zoom in...");
    }
    private void ZoomOut()
    {
        justBrainandTumor.transform.localScale += new Vector3(-0.05f, -0.05f, -0.05f);
        Debug.Log("Zoom out...");
    }
    public bool IsEven(int num)
    {
        return num % 2 == 0;
    }
}
EricBatlle commented 1 year ago

Hi @romygt , help me in order to help you, do not simply copy your code and say "my code doesn't work and I don't know why" ^^'

Try to attach some logcat, error log, tell me what your code changes are supposed to do, etc

EricBatlle commented 1 year ago

Since there haven't been any recent updates, I am going to close this issue.

@romygt if you're still experiencing this problem and want to continue the discussion just leave a comment here and I'll be happy to re-open this.