Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I created a Java plugin to include speech recognition in my Unity Android game. It is activated by touching a button onscreen, and when the button is released, the audio is processed. This feature isn't working, and checking the game logs I found I am getting a ClassNotFoundException related to the plugin's class.

My plugin is a JAR file located at Assets/Plugins/Android inside my Unity project. Here is the code that it contains:


Java
package com.example.unityandroidplswork;

import android.content.Intent;
import android.app.Activity;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.util.Log;

import java.util.ArrayList;


public class STTPlugin {
    private static final int REQUEST_CODE = 1234;
    private static final String TAG = "SpeechRecognitionPlugin";
    private Activity activity;
    private SpeechRecognizer speechRecognizer;
    private SpeechRecognitionResultListener resultListener;

    public STTPlugin(Activity activity) {

        this.activity = activity;
    }

    public void startSpeechRecognition(SpeechRecognitionResultListener listener) {
        if (SpeechRecognizer.isRecognitionAvailable(activity.getApplicationContext())) {
            resultListener = listener;
            if (speechRecognizer == null) {
                speechRecognizer = SpeechRecognizer.createSpeechRecognizer(activity.getApplicationContext());
            }
            Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
            intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 10);
            speechRecognizer.startListening(intent);
            Log.d(TAG, "Escuchando...");
        } else {
            Log.d(TAG, "Reconocimiento no disponible.");
        }

    }

    public void stopSpeechRecognition() {
        if (speechRecognizer != null) {
            speechRecognizer.stopListening();
        }
    }

    private String[] processRecognitionResults(ArrayList<String> results) {
        return results.toArray(new String[results.size()]);
    }

    public void handleActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) {
            ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            if (resultListener != null) {
                String[] resultsArray = processRecognitionResults(results);
                resultListener.onResults(resultsArray);
            }
        }
    }

    public interface SpeechRecognitionResultListener {
        void onResults(String[] results);
    }


}


And the C# script from which I call the plugin:

C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.UI;
using TMPro;

public class SpeechRecog : MonoBehaviour
{

    private AndroidJavaObject speechRecPlugin;
    public TextMeshProUGUI textofresult;

    // Start is called before the first frame update
    void Start()
    {
        AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
        speechRecPlugin = new AndroidJavaObject("com.example.unityandroidplswork.STTPlugin", activity);
    }

    public void StartSpeechRecognition() {
        AndroidJavaObject speechResultListener = new AndroidJavaObject("com.example.unityandroidplswork.STTPlugin$SpeechRecognitionResultListener", this);
     
        speechRecPlugin.Call("startSpeechRecognition", speechResultListener);
    }

    public void StopSpeechRecognition() {
        speechRecPlugin.Call("stopSpeechRecognition");
    }

    private void ProcessRecognitionResults(string[] results) {
        string recognizedtext = "";
        foreach(string result in results) {
            Debug.Log("Result: " + result);
            recognizedtext += result + " ";
        }
        textofresult.text = recognizedtext;
        //process here for sure
        //ProcessCommands(results)
    } 


    private class SpeechRecognitionResultListener : AndroidJavaProxy {

        private SpeechRecog plugin;

        public SpeechRecognitionResultListener(SpeechRecog plugin) : base("com.example.unityandroidplswork.STTPlugin$SpeechRecognitionResultListener") {
            this.plugin = plugin;
        }

        public void onResults(string[] results) {
            plugin.ProcessRecognitionResults(results);
        }
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}


What I have tried:

I searched about this problem and I tried proposed solutions such as reimporting the plugin, changing its location, unchecking the release field in the publishing settings... But none of this has worked for me.
Posted
Updated 20-Jun-23 22:28pm
v2
Comments
[no name] 19-Jun-23 13:33pm    
Use C# if Java doesn't work for you.

https://stackoverflow.com/questions/39611728/how-to-add-speech-recognition-to-unity-project

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900