语音识别不适用于 Android 5.1.1 Xamarin

Posted

技术标签:

【中文标题】语音识别不适用于 Android 5.1.1 Xamarin【英文标题】:Speech recognition not working on Android 5.1.1 Xamarin 【发布时间】:2020-11-14 16:16:12 【问题描述】:

我正在尝试使用离线语音识别引擎来检测短语以激活我的应用上的命令。

[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, ScreenOrientation = ScreenOrientation.Landscape)]
public class MainActivity : AppCompatActivity, IRecognitionListener

    Intent mSpeechRecognizerIntent;

    public override async void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
    
        Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);


        if (grantResults.Where(x => x == Permission.Denied).Count() == 0)
        
            SpeechRecognizer speechRecognizer = SpeechRecognizer.CreateSpeechRecognizer(this);
            speechRecognizer.SetRecognitionListener(this);

            mSpeechRecognizerIntent = new Intent(RecognizerIntent.ActionRecognizeSpeech);
            //mSpeechRecognizerIntent = new Intent(RecognizerIntent.ActionVoiceSearchHandsFree);


            mSpeechRecognizerIntent.PutExtra(RecognizerIntent.ExtraCallingPackage, PackageName);
            mSpeechRecognizerIntent.PutExtra(RecognizerIntent.ExtraPreferOffline, true);
            mSpeechRecognizerIntent.PutExtra(RecognizerIntent.ExtraPartialResults, true);
            mSpeechRecognizerIntent.PutExtra(RecognizerIntent.ExtraMaxResults, 5);
            mSpeechRecognizerIntent.PutExtra(RecognizerIntent.ExtraLanguageModel, "en");


            int max = 30000;

            //mSpeechRecognizerIntent.PutExtra(RecognizerIntent.ExtraLanguageModel, this.Resources.Configuration.Locale.Language);
            //mSpeechRecognizerIntent.PutExtra(RecognizerIntent.ExtraPrompt, "test");
            //mSpeechRecognizerIntent.PutExtra(RecognizerIntent.ExtraMaxResults, max);
            //mSpeechRecognizerIntent.PutExtra(RecognizerIntent.ExtraSpeechInputCompleteSilenceLengthMillis, max);
            ////mSpeechRecognizerIntent.PutExtra(RecognizerIntent.ExtraSpeechInputMinimumLengthMillis, max);
            //mSpeechRecognizerIntent.PutExtra(RecognizerIntent.ExtraSpeechInputPossiblyCompleteSilenceLengthMillis, max);

            speechRecognizer.StartListening(mSpeechRecognizerIntent);


        

        base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    


    public void OnBeginningOfSpeech()
    

    

    public void OnBufferReceived(byte[] buffer)
    

    

    public void OnEndOfSpeech()
    

    

    public void OnError([GeneratedEnum] SpeechRecognizerError error)
    
        Debugger.Log(0, "cat", "Speech error: " + error);
    

    public void OnEvent(int eventType, Bundle @params)
    

    

    public void OnPartialResults(Bundle partialResults)
    
        List<string> matches = partialResults.GetStringArrayList(SpeechRecognizer.ResultsRecognition).ToList();

        Debugger.Log(0, "cat", "Partial Matches are: " + string.Join(", ", matches.ToArray()) + "\r\n");

    

    public void OnReadyForSpeech(Bundle @params)
    

    

    public void OnResults(Bundle results)
    
        List<string> matches = results.GetStringArrayList(SpeechRecognizer.ResultsRecognition).ToList();

        Debugger.Log(0, "cat", "Matches are: " + string.Join(", ", matches.ToArray()) + "\r\n");
    

    public void OnRmsChanged(float rmsdB)
    

    

它适用于我的 android 7.0 手机,但没有任何回调在我的 android 5.1.1 手机上触发。

当我在模拟器上尝试时。它给出了SpeechRecognizerError.Server 错误。

模拟器也不允许我下载任何离线引擎。

Android 5.1.1 手机确实下载了引擎,可以在 wifi 和数据关闭的情况下进行语音识别,但在我的应用程序中没有。

我该如何解决这个问题?

【问题讨论】:

IsRecognitionAvailable 返回什么? @Cheesebaron 它返回 true SpeechRecognizer "方法只能从主应用线程调用" 语音识别器offline workaround 【参考方案1】:

mSpeechRecognizerIntent.PutExtra(RecognizerIntent.ExtraPreferOffline, true);

这看起来会破坏您的代码,不确定但可能 Google 不支持它。 尝试对此发表评论。 //mSpeechRecognizerIntent.PutExtra(RecognizerIntent.ExtraPreferOffline, true);

【讨论】:

以上是关于语音识别不适用于 Android 5.1.1 Xamarin的主要内容,如果未能解决你的问题,请参考以下文章

java 用于android的语音识别器(语音到文本)

检测 MDNS/Zeroconf 设备适用于 Android 5.1.1,但不适用于 Android 7

android如何自定义语音识别

语音识别和声音与音乐的比较

EditText中的标点符号 - Android Nougat

Android语音播报后台播报语音识别