2017-07-17 54 views
2

我想禁用Google語音彈出窗口。我想讓它在後臺運行,所以不會出現彈出窗口。我在Visual Studio中用c#編程,我想在Android上/在Android上創建語音識別應用程序。禁用Google語音彈出窗口

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); 
} 

有人可以幫我關閉彈出?

+0

對不起,這是我的錯。我的意思是彈出窗口不應該出現,所以如果我按下按鈕,它應該開始聽(無彈出)。 – Ossi

回答

1

您可以實現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在你的應用程序。