2017-08-08 142 views
-2

工作:我想喜歡「當我打電話,它會啓動語音識別方法,並給出語音識別的結果沒有彈出谷歌語音識別在Android Studio中

問題的應用:語音識別器,當我開始打電話該方法,但它不是給語音識別瞬間的結果它給在第二呼叫第一個呼召的結果

我的代碼:。 語音識別類:

package com.ooo.voicerecog; 
import java.util.ArrayList; 
import android.Manifest; 
import android.content.pm.PackageManager; 
import android.os.Build; 
import android.speech.RecognitionListener; 
import android.speech.RecognizerIntent; 
import android.speech.SpeechRecognizer; 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.v4.app.ActivityCompat; 
import android.support.v4.content.ContextCompat; 
import android.util.Log; 
public class VoiceRecognitionActivity extends Activity implements   
RecognitionListener { 
private SpeechRecognizer speech = null; 
private Intent recognizerIntent; 
public ArrayList<String> matches; 
public String newtext=null; 
public String text =null; 
public void starto(){ 
int MyVersion = Build.VERSION.SDK_INT; 
if (MyVersion > Build.VERSION_CODES.LOLLIPOP_MR1) { 
    if (!checkIfAlreadyhavePermission()) { 
     requestForSpecificPermission(); 
    } 
} 
speech = SpeechRecognizer.createSpeechRecognizer(this); 
speech.setRecognitionListener(this); 
recognizerIntent = new 
Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, 
     "en"); 
recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, 
     this.getPackageName()); 
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, 
     RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH); 
recognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3); 
speech.startListening(recognizerIntent); 
} 
public String getvoice(){ 
starto(); 
speech.stopListening(); 
return text; 
} 
private boolean checkIfAlreadyhavePermission() { 
int result = ContextCompat.checkSelfPermission(this, 
Manifest.permission.RECORD_AUDIO); 
if (result == PackageManager.PERMISSION_GRANTED) { 
return true; 
} else { 
return false;} 
} 

@Override 
public void onRequestPermissionsResult(int requestCode, String[] 
permissions, int[] grantResults) { 
switch (requestCode) { 
case 101: 
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
       //granted 
} else { 
       //not granted 
} 
break; 
default: 
super.onRequestPermissionsResult(requestCode, permissions, 
grantResults); 
    } 
} 
private void requestForSpecificPermission() { 
ActivityCompat.requestPermissions(this, new String[] 
{Manifest.permission.RECORD_AUDIO}, 101); 
} 
@Override 
public void onBeginningOfSpeech() { } 

@Override 
public void onBufferReceived(byte[] buffer) { } 

@Override 
public void onEndOfSpeech() {} 

@Override 
public void onError(int errorCode) { 
    String errorMessage = getErrorText(errorCode); 
} 

@Override 
public void onEvent(int arg0, Bundle arg1) {} 

@Override 
public void onPartialResults(Bundle arg0) {} 

@Override 
public void onReadyForSpeech(Bundle arg0) { } 

@Override 
public void onResults(Bundle results) { 
matches = 
results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); 
    for (String result : matches) 
     text += result + "\n";} 

@Override 
public void onRmsChanged(float rmsdB) {} 

public static String getErrorText(int errorCode) { 
    String message; 
    switch (errorCode) { 
     case SpeechRecognizer.ERROR_AUDIO: 
      message = "Audio recording error"; 
      break; 
     case SpeechRecognizer.ERROR_CLIENT: 
      message = "Client side error"; 
      break; 
     case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS: 
      message = "Insufficient permissions"; 
      break; 
     case SpeechRecognizer.ERROR_NETWORK: 
      message = "Network error"; 
      break; 
     case SpeechRecognizer.ERROR_NETWORK_TIMEOUT: 
      message = "Network timeout"; 
      break; 
     case SpeechRecognizer.ERROR_NO_MATCH: 
      message = "No match"; 
      break; 
     case SpeechRecognizer.ERROR_RECOGNIZER_BUSY: 
      message = "RecognitionService busy"; 
      break; 
     case SpeechRecognizer.ERROR_SERVER: 
      message = "error from server"; 
      break; 
     case SpeechRecognizer.ERROR_SPEECH_TIMEOUT: 
      message = "No speech input"; 
      break; 
     default: 
      message = "Didn't understand, please try again."; 
      break; 
    } 
    return message; 
    } 

    } 

MainActivity:

package com.ooo.voicerecog; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class MainActivity extends VoiceRecognitionActivity{ 
public Button button; 
public Button button1; 
public TextView textView; 
public String texo; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    button = (Button) findViewById(R.id.button); 
    textView = (TextView) findViewById(R.id.textview); 
    button.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View view) { 

      textView.setText(getvoice()); 



     } 
    }); 




    }} 
+0

我不知道是什麼問題,你遇到或疑問,你問。請[編輯]你的問題,並**是特定的**,並清楚地解釋問題,並提出**具體問題**(*這不正確*不具體)。見[問]。 –

回答

0

你可以這樣做。它不會打開谷歌提示並開始收聽。下面一行是卓有成效的

speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 

代碼:

speechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
    speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
    speechIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, context.getPackageName()); 
    // for more: https://stackoverflow.com/questions/7973023/what-is-the-list-of-supported-languages-locales-on-android 
    /*intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en_IN"); */ 
    speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault()); 
    speechIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1); 
    speechIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, 10000); 
    speechIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS, new Long(5000)); 
    speechIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS, new Long(5000)); 
    speechIntent.putExtra("android.speech.extra.DICTATION_MODE", false); 
    speechIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true); 
    /* 
    intent.putExtra("android.speech.extra.GET_AUDIO_FORMAT", "audio/AMR"); 
    intent.putExtra("android.speech.extra.GET_AUDIO", true); 
    */ 
    isRecognizerRunning = true; 
    sr.startListening(speechIntent); 

和使用自定義類偵聽到它

class listener implements RecognitionListener { 

    public void onReadyForSpeech(Bundle params) { 
     CommonUtilities._Log(CommonUtilities.logs.e, TAG, "onReadyForSpeech"); 
    } 

    public void onBeginningOfSpeech() { 
     CommonUtilities._Log(CommonUtilities.logs.e, TAG, "onBeginningOfSpeech"); 
    } 

    public void onRmsChanged(float rmsdB) { 

    } 

    public void onBufferReceived(byte[] buffer) { 
     CommonUtilities._Log(CommonUtilities.logs.e, TAG, "onBufferReceived"); 
    } 

    public void onEndOfSpeech() { 
     CommonUtilities._Log(CommonUtilities.logs.e, TAG, "onEndofSpeech"); 
     isRecognizerRunning = false; 
    } 

    public void onError(int error) { 
     String message; 
     switch (error) { 
      case SpeechRecognizer.ERROR_AUDIO: 
       message = "Audio recording error"; 
       break; 
      case SpeechRecognizer.ERROR_CLIENT: 
       message = "Client side error"; 
       break; 
      case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS: 
       message = "Insufficient permissions"; 
       break; 
      case SpeechRecognizer.ERROR_NETWORK: 
       message = "Network error"; 
       break; 
      case SpeechRecognizer.ERROR_NETWORK_TIMEOUT: 
       message = "Network timeout"; 
       break; 
      case SpeechRecognizer.ERROR_NO_MATCH: 
       message = "No match"; 
       break; 
      case SpeechRecognizer.ERROR_RECOGNIZER_BUSY: 
       message = "RecognitionService busy"; 
       break; 
      case SpeechRecognizer.ERROR_SERVER: 
       message = "error from server"; 
       break; 
      case SpeechRecognizer.ERROR_SPEECH_TIMEOUT: 
       message = "No speech input"; 
       break; 
      default: 
       message = "Didn't understand, please try again."; 
       break; 
     } 

    } 

    public void onResults(Bundle results) { 
     String str = ""; 
     ArrayList data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); 
     for (int i = 0; i < data.size(); i++) { 
      CommonUtilities._Log(CommonUtilities.logs.e, TAG, "onResults result " + data.get(i) + " size " + data.size()); 
      str += data.get(i); 
     } 
     if (!data.get(0).toString().trim().isEmpty()) { 
      checkAndPerformAction(data.get(0).toString());  // best match 
     } 
    } 

    public void onPartialResults(Bundle partialResults) { 
     String str = ""; 
     ArrayList data = partialResults.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); 
     CommonUtilities._Log(CommonUtilities.logs.e, TAG, "onPartialResults " + data.get(0).toString()); 
     String[] partial = data.get(0).toString().split(" "); 
     if (partial.length > previousPartialResults.length) { 
      if (partial.length == 1 && !partial[0].trim().isEmpty()) { 
       previousPartialResults = partial; 
       CommonUtilities._Log(CommonUtilities.logs.e, TAG, "onPartialResults New " + partial[0].trim()); 
      } else if (partial.length != 1) { 
       previousPartialResults = partial; 
       CommonUtilities._Log(CommonUtilities.logs.e, TAG, "onPartialResults New " + previousPartialResults[previousPartialResults.length - 1]); 
      } 
     } 
     CommonUtilities._Log(CommonUtilities.logs.e, TAG, "onPartialResults final " + str); 
    } 

    public void onEvent(int eventType, Bundle params) { 
     CommonUtilities._Log(CommonUtilities.logs.e, TAG, "onEvent " + eventType); 
    } 
} 
+0

該應用程序已經工作,沒有彈出。我的問題不是這個 –

+2

@BoraySaygılıer:然後編輯你的問題和**更清楚**關於你有什麼問題。 –