语音识别在 Android 5.1.1 的 Xamarin 上无法工作。

huangapple 未分类评论42阅读模式
英文:

Speech recognition not working on Android 5.1.1 Xamarin

问题

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

[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 OnError([GeneratedEnum] SpeechRecognizerError error)
    {
        Debugger.Log(0, "cat", "Speech error: " + error);
    }

    // 省略其他回调方法...
}

在我的 Android 7.0 手机上可以运行,但在 Android 5.1.1 手机上没有触发任何回调。

当我在模拟器上尝试时,会出现 SpeechRecognizerError.Server 错误。

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

Android 5.1.1 手机已经下载了引擎,并且在关闭 Wi-Fi 和数据连接的情况下可以进行语音识别,但在我的应用中却无法使用。

如何解决这个问题?

英文:

I am trying to use the offline speech recognition engine to detect a phrase to activate a command on my app.

[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)
    {

    }
}

It works on my android 7.0 phone, but none of the callbacks fire on my android 5.1.1 phone.

Also when I try it on an emulator. It gives the SpeechRecognizerError.Server error.

The emulator also doesn't allow me to download any offline engines.

The Android 5.1.1 phone does have engines downloaded and works for speech recognition with both wifi and data off, but not in my application.

How can I fix this issue?

答案1

得分: 0

mSpeechRecognizerIntent.PutExtra(RecognizerIntent.ExtraPreferOffline, true);

这段代码可能会导致您的代码出现问题不能确定但可能是因为 Google 不再支持它
尝试将其注释掉
//mSpeechRecognizerIntent.PutExtra(RecognizerIntent.ExtraPreferOffline, true);
英文:

mSpeechRecognizerIntent.PutExtra(RecognizerIntent.ExtraPreferOffline, true);

this looks to breaking your code, not sure but probably Google is not supporting it.
try to comment this out.
//mSpeechRecognizerIntent.PutExtra(RecognizerIntent.ExtraPreferOffline, true);

huangapple
  • 本文由 发表于 2020年7月25日 02:38:32
  • 转载请务必保留本文链接:https://java.coder-hub.com/63079640.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定