c# - 禁用 Google 语音的弹出窗口

标签 c# android visual-studio xamarin speech-to-text

我想禁用 Google 语音的弹出窗口。我希望它在后台运行,这样就不会出现弹出窗口。我正在 Visual Studio 中使用 C# 进行编程,我想在 Android 上/在 Android 中制作一个语音识别应用程序。

I downloaded the sample code from the Xamarin page

代码:

protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);

    // set the isRecording flag to false (not recording)
    isRecording = false;

    // Set our view from the "main" layout resource
    SetContentView(Resource.Layout.Main);

    // get the resources from the layout
    recButton = FindViewById<Button>(Resource.Id.btnRecord);
    textBox = FindViewById<TextView>(Resource.Id.textYourText);


    // check to see if we can actually record - if we can, assign the event to the button
    string rec = Android.Content.PM.PackageManager.FeatureMicrophone;
    if (rec != "android.hardware.microphone")
    {
        // no microphone, no recording. Disable the button and output an alert
        var alert = new AlertDialog.Builder(recButton.Context);
        alert.SetTitle("You don't seem to have a microphone to record with");
        alert.SetPositiveButton("OK", (sender, e) =>
        {
            textBox.Text = "No microphone present";
            recButton.Enabled = false;
            return;
        });

        alert.Show();
    }
    else
    {
        recButton.Click += delegate
        {
            // change the text on the button
            recButton.Text = "End Recording";
            isRecording = !isRecording;
            if (isRecording)
            {
                // create the intent and start the activity
                var voiceIntent = new Intent(RecognizerIntent.ActionRecognizeSpeech);
                voiceIntent.PutExtra(RecognizerIntent.ExtraLanguageModel, RecognizerIntent.LanguageModelFreeForm);

                // put a message on the modal dialog
                voiceIntent.PutExtra(RecognizerIntent.ExtraPrompt, Application.Context.GetString(Resource.String.messageSpeakNow));

                // if there is more then 1.5s of silence, consider the speech over
                voiceIntent.PutExtra(RecognizerIntent.ExtraSpeechInputCompleteSilenceLengthMillis, 1500);
                voiceIntent.PutExtra(RecognizerIntent.ExtraSpeechInputPossiblyCompleteSilenceLengthMillis, 1500);
                voiceIntent.PutExtra(RecognizerIntent.ExtraSpeechInputMinimumLengthMillis, 15000);
                voiceIntent.PutExtra(RecognizerIntent.ExtraMaxResults, 1);
                voiceIntent.PutExtra(RecognizerIntent.ExtraLanguage, Java.Util.Locale.German);


                // you can specify other languages recognised here, for example

                // if you wish it to recognise the default Locale language and German
                // if you do use another locale, regional dialects may not be recognised very well

                voiceIntent.PutExtra(RecognizerIntent.ExtraLanguage, Java.Util.Locale.Default);
                StartActivityForResult(voiceIntent, VOICE);
            }
        };
    }
}

protected override void OnActivityResult(int requestCode, Result resultVal, Intent data)
{
    if (requestCode == VOICE)
    {
        if (resultVal == Result.Ok)
        {
            var matches = data.GetStringArrayListExtra(RecognizerIntent.ExtraResults);
            if (matches.Count != 0)
            {
                string textInput = textBox.Text + matches[0];

                // limit the output to 500 characters
                if (textInput.Length > 500)
                    textInput = textInput.Substring(0, 500);
                textBox.Text = textInput;
            }
            else
                textBox.Text = "No speech was recognised";
            // change the text back on the button
            recButton.Text = "Start Recording";
        }
    }
    base.OnActivityResult(requestCode, resultVal, data);
}

有人可以帮我禁用弹出窗口吗?

最佳答案

您可以为您的应用实现SpeechRecognizer。该 API 的实现可能会将音频流式传输到远程服务器以执行语音识别。

您的应用需要拥有 RECORD_AUDIO 权限。

例如:

public class MainActivity : Activity, IRecognitionListener
{
    private TextView tv;
    private SpeechRecognizer sr;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);

        sr = SpeechRecognizer.CreateSpeechRecognizer(this);
        sr.SetRecognitionListener(this);

        Button btn = FindViewById<Button>(Resource.Id.btn);
        btn.Click += (sender, e) =>
        {
            Intent intent = new Intent(RecognizerIntent.ActionRecognizeSpeech);
            intent.PutExtra(RecognizerIntent.ExtraLanguageModel, RecognizerIntent.LanguageModelFreeForm);
            intent.PutExtra(RecognizerIntent.ExtraCallingPackage, "this package");
            intent.PutExtra(RecognizerIntent.ExtraMaxResults, 5);
            sr.StartListening(intent);
        };

        tv = FindViewById<TextView>(Resource.Id.tv);
    }

    public void OnBeginningOfSpeech()
    {
        tv.Text = "Beginning";
    }

    public void OnBufferReceived(byte[] buffer)
    {
    }

    public void OnEndOfSpeech()
    {
    }

    public void OnError([GeneratedEnum] SpeechRecognizerError error)
    {
        tv.Text = error.ToString();
    }

    public void OnEvent(int eventType, Bundle @params)
    {
    }

    public void OnPartialResults(Bundle partialResults)
    {
    }

    public void OnReadyForSpeech(Bundle @params)
    {
        tv.Text = "Ready!";
    }

    public void OnResults(Bundle results)
    {
        var data = results.GetStringArrayList(SpeechRecognizer.ResultsRecognition);
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < data.Count; i++)
        {
            builder.Append(data[i]);
        }
        tv.Text = builder.ToString();
    }

    public void OnRmsChanged(float rmsdB)
    {
    }
}

这仍然使用谷歌的语音服务,如果您的设备/地区不支持此服务,您将需要找到其他语音服务并在您的应用中使用他们的 API。

关于c# - 禁用 Google 语音的弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45138873/

相关文章:

安卓 : how to execute AsyncTask?

安卓拍照成功

c# - 在 PInvoke 中管理大量 3rdparty 依赖项以获得更好的编译速度

visual-studio - 在 Visual Studio 中按项目过滤错误/警告

c# - 运算符 '&&' 不能应用于类型 'bool' 和 'int' 的操作数

c# - TDD - 使用属性自动生成代码

c# - 当我从任务中检索值时,如何更新父窗体上的 GUI?

java - 选择查询星期几?

控制台应用程序引用的 C# 版本控制

c# - LINQ to Entity Framework 多对多预加载问题