2013-02-01 81 views
0

我想在Android上使用SpeechRecognizer類並影響它的RecognitionListener進行語音識別。Android監聽器語音識別不叫

問題:受影響的偵聽器未被調用。

這裏是我的主要活動代碼:

package com.example.testvoicepaper; 

import java.util.ArrayList; 

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.speech.RecognitionListener; 
import android.speech.RecognizerIntent; 
import android.speech.SpeechRecognizer; 
import android.util.Log; 
import android.view.KeyEvent; 
import android.view.Menu; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.View.OnKeyListener; 
import android.view.View.OnTouchListener; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class MainActivity extends Activity { 

public int VOICE_RECOGNITION_REQUEST_CODE = 1; 
private Button launchRecognition; 
private TextView textRecognised; 
private ImageView picture; 
private SpeechRecognizer sr; 
private Context context; 
private MyRecognitionListener listener; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    textRecognised = (TextView) findViewById(R.id.textRecognised); 
    launchRecognition = (Button) findViewById(R.id.launchRecognition); 
    picture = (ImageView) findViewById(R.id.picture); 
    context = this; 

    sr = SpeechRecognizer.createSpeechRecognizer(getApplicationContext()); 
    listener = new MyRecognitionListener(); 
    sr.setRecognitionListener(listener); 

    launchRecognition.setOnTouchListener(new OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 

      switch (event.getAction()) { 

      case MotionEvent.ACTION_DOWN: { 
       sr.startListening(RecognizerIntent.getVoiceDetailsIntent(context)); 
       listener.onBeginningOfSpeech(); 
       break; 
      } 

      case MotionEvent.ACTION_UP: { 
       listener.onEndOfSpeech(); 
       sr.stopListening(); 
       break; 
      } 
      } 
      return false; 

     } 
    }); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 

class MyRecognitionListener implements RecognitionListener { 

    @Override 
    public void onBeginningOfSpeech() { 
     Log.d("Speech", "onBeginningOfSpeech"); 
    } 

    @Override 
    public void onBufferReceived(byte[] buffer) { 
     Log.d("Speech", "onBufferReceived"); 
    } 

    @Override 
    public void onEndOfSpeech() { 
     Log.d("Speech", "onEndOfSpeech"); 
    } 

    @Override 
    public void onError(int error) { 
     Log.d("Speech", "onError" + error); 
    } 

    @Override 
    public void onEvent(int eventType, Bundle params) { 
     Log.d("Speech", "onEvent"); 
    } 

    @Override 
    public void onPartialResults(Bundle partialResults) { 
     Log.d("Speech", "onPartialResults"); 
    } 

    @Override 
    public void onReadyForSpeech(Bundle params) { 
     Log.d("Speech", "onReadyForSpeech"); 
    } 


    @Override 
    public void onResults(Bundle results) { 
     Log.d("Speech", "onResults"); 
     ArrayList strlist = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); 
     for (int i = 0; i < strlist.size();i++) { 
      Log.d("Speech", "result=" + strlist.get(i)); 
     } 
    } 

    @Override 
    public void onRmsChanged(float rmsdB) { 
     Log.d("Speech", "onRmsChanged"); 
    } 

} 
} 

它調用beginningOfSpeech()和endOfSpeech(),但從來沒有監聽功能。

+0

你有答案嗎?我有同樣的問題。 –

回答

0

這是錯誤Intent使用

RecognizerIntent.getVoiceDetailsIntent(context)) 

你想要的東西,像

Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, 
      RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH); 
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, prompt); 

退房GAST對多種原材料的東西一樣this class

0

嘗試在您的MainActivity實施RecognitionListener代替創建'MyRecognitionListener'。

1

確保您的應用已啓用RECORD_AUDIO權限。