This simple Unity package uses a workaround to enable continuous speech recognition on Android devices.
You have two options:
- Download the Unity package: You can download the Unity package here.
- Clone the repository: Clone this project and drag and drop the folder SpeechRecognizer located at UnitySpeechSample/Assets into your Unity project.
- Attach SpeechRecognizer script: Attach the SpeechRecognizer script to any game object in your Unity project.
- Set properties: In the Inspector, set the language and maxResults properties as per your requirements.
- Use the singleton instance:
using UnityEngine;
// import SpeechRecognizer
using Jplumi.SpeechRecognizer;
public class SpeechManager : MonoBehaviour
{
private SpeechRecognizer recognizer;
private void Start()
{
// Use the SpeechRecognizer singleton
recognizer = SpeechRecognizer.Instance;
// OnResults will be invoked when the whole speech is ready
recognizer.OnResults += OnRecognizerResults;
// OnResults will be invoked when partial results are available
recognizer.OnPartialResults += OnRecognizerPartialResults;
}
public void StartRecognizer()
{
recognizer.StartListening();
}
public void StopRecognizer()
{
recognizer.StopListening();
}
public void OnRecognizerResults(string result)
{
Debug.Log("Recognizer result: " + result);
}
public void OnRecognizerPartialResults(string result)
{
Debug.Log("Recognizer partial result: " + result);
}
private void OnDisable()
{
// Unregister event listeners to avoid memory leaks
recognizer.OnResults -= OnRecognizerResults;
recognizer.OnPartialResults -= OnRecognizerPartialResults;
}
}