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:
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:
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;
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;
}
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);
}
}
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.