2017-06-23 58 views
0

我在我的應用程序中使用recognitionlistener界面將語音轉換爲文本。不過,我想在演講後立即顯示來自演講的轉換文本。如果我說4-5個單詞,那麼只要我說一個單詞就應該顯示第一個單詞。總之就像谷歌助理。我正在重寫它的onresults方法並顯示調用此方法時的結果。有沒有辦法一次檢測出一個單詞並逐一顯示?Android(RecognitionListener)現場演講文本預覽

https://drive.google.com/open?id=0BxHdMJXrMP2HRk80aVBtZHIxaDAenter link description here

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.speech.RecognitionListener; 
import android.speech.RecognizerIntent; 
import android.speech.SpeechRecognizer; 
import android.widget.Button; 
import android.widget.TextView; 
import java.util.ArrayList; 
import android.util.Log; 



public class VoiceRecognitionTest extends Activity implements OnClickListener 
{ 

    private TextView mText; 
    private SpeechRecognizer sr; 
    private static final String TAG = "MyActivity"; 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 
      Button speakButton = (Button) findViewById(R.id.btn_speak);  
      mText = (TextView) findViewById(R.id.textView1);  
      speakButton.setOnClickListener(this); 
      sr = SpeechRecognizer.createSpeechRecognizer(this);  
      sr.setRecognitionListener(new listener());   
    } 

    class listener implements RecognitionListener   
    { 
      public void onReadyForSpeech(Bundle params) 
      { 
        Log.d(TAG, "onReadyForSpeech"); 
      } 
      public void onBeginningOfSpeech() 
      { 
        Log.d(TAG, "onBeginningOfSpeech"); 
      } 
      public void onRmsChanged(float rmsdB) 
      { 
        Log.d(TAG, "onRmsChanged"); 
      } 
      public void onBufferReceived(byte[] buffer) 
      { 
        Log.d(TAG, "onBufferReceived"); 
      } 
      public void onEndOfSpeech() 
      { 
        Log.d(TAG, "onEndofSpeech"); 
      } 
      public void onError(int error) 
      { 
        Log.d(TAG, "error " + error); 
        mText.setText("error " + error); 
      } 
      public void onResults(Bundle results)     
      { 
        String str = new String(); 
        Log.d(TAG, "onResults " + results); 
        ArrayList data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); 
        for (int i = 0; i < data.size(); i++) 
        { 
           Log.d(TAG, "result " + data.get(i)); 
           str += data.get(i); 
        } 
        mText.setText("results: "+String.valueOf(data.size()));   
      } 
      public void onPartialResults(Bundle partialResults) 
      { 
        Log.d(TAG, "onPartialResults"); 
      } 
      public void onEvent(int eventType, Bundle params) 
      { 
        Log.d(TAG, "onEvent " + eventType); 
      } 
    } 
    public void onClick(View v) { 
      if (v.getId() == R.id.btn_speak) 
      { 
       Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);   
       intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
       intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"voice.recognition.test"); 

       intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5); 
        sr.startListening(intent); 

      } 
    } 
} 
+1

添加一些代碼... –

回答

2

當然可以,你需要重寫onPartialResults :)

@Override 
      public void onPartialResults(Bundle partialResults) { 
       ArrayList data = partialResults.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); 
       String word = (String) data.get(data.size() - 1); 
       recognisedText.setText(word); 

       Log.i("TEST", "partial_results: " + word); 
      } 
+1

除了這個,我必須設置RecognizerIntent.EXTRA_PARTIAL_RESULTS到真正。謝謝! –

+0

我很高興你設法做到了! :) –