【问题标题】:Voice Recognition for Android in UnityUnity 中的 Android 语音识别
【发布时间】:2022-12-05 02:50:06
【问题描述】:
我正在寻找一种方法让 Unity 在 Android 构建中识别用户的语音。我找到了适用于 Windows 的解决方案:youtube.com/watch?v=29vyEOgsW8s&t=612s,但我需要适用于 Android。我不需要它将语音转换为文本,我只希望在正确发音的单词之后出现一个小图像。将不胜感激任何建议,谢谢!我已经尝试了一些东西,但没有用,而且我在 C# 方面也不是很好。尽管如此,还是会很乐意接受任何帮助。
【问题讨论】:
标签:
c#
unity3d
unityscript
voice-recognition
android-build
【解决方案1】:
您引用的视频使用 Windows.Speech API,对于 Android,您可能想要使用 Android.Speech 包。我不知道情况是否仍然如此,但您可能需要将以下内容添加到清单文件中才能使用它:
<intent>
<action android:name="android.speech.RecognitionService" />
</intent>
至于 Unity 集成,unity 确实有一个内置的 Microphone 类,或者如果您可以访问 Android 包:
private const int Voice = 10;
private string _recognizedText;
private void Start()
{
// Check if the device supports speech recognition
if (!Android.Speech.Recognition.IsRecognitionAvailable(this))
{
Debug.LogError("Speech recognition is not available on this device!");
return;
}
// Create a new intent for speech recognition
var intent = new Intent(RecognizerIntent.ActionRecognizeSpeech);
// Set the language for the intent
intent.PutExtra(RecognizerIntent.ExtraLanguage, Java.Util.Locale.Default);
// Start the activity for speech recognition
StartActivityForResult(intent, Voice);
}
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (requestCode == Voice && resultCode == Result.Ok)
{
// Get the recognized text from the intent
_recognizedText = data.GetStringExtra(RecognizerIntent.ExtraResultsRecognition);
Debug.Log("Recognized text: " + _recognizedText);
}
}