无法将android.speech实现到surfaceview中

时间:2013-11-18 14:22:14

标签: android surfaceview

我正在尝试实施

  

android.speech

在表面视图中,但我无法弄清楚如何使用

  

新意图(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

在案例

  

MotionEvent.ACTION_DOWN:

任何帮助都将受到赞赏n.n

我已经看过这篇文章“Speech Recognition in Surface View [android]”,但它并没有完全回答我的问题u.u

1 个答案:

答案 0 :(得分:0)

您可以编写自己的RecognitionListener实现。

以下是在启动时立即识别语音的Activity的示例。如果您在SurfaceView中执行此操作,则需要在创建语音识别器时传递SurfaceView的上下文。

public class BackgroundListenerTest extends Activity implements RecognitionListener {
  /** Text display */
  private TextView blurb;

  /** Parameters for recognition */
  private Intent recognizerIntent;

  /** The ear */
  private SpeechRecognizer recognizer;

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

    blurb = (TextView) findViewById(R.id.blurb);

    recognizer = SpeechRecognizer.createSpeechRecognizer(this);
    recognizer.setRecognitionListener(this);

    recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, "com.example.yourpackage");
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
    recognizer.startListening(recognizerIntent);
  }

  @Override
  public void onBeginningOfSpeech() {
    blurb.append("[");
  }

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

  @Override
  public void onEndOfSpeech() {
    blurb.append("] ");
  }

  @Override
  public void onError(int arg0) {
  }

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

  @Override
  public void onPartialResults(Bundle arg0) {
  }

  @Override
  public void onReadyForSpeech(Bundle arg0) {
    blurb.append("> ");
  }

  @Override
  public void onResults(Bundle bundle) {
    ArrayList<String> results = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
    blurb.append(results.toString() + "\n");
    recognizer.startListening(recognizerIntent);
  }

  @Override
  public void onRmsChanged(float arg0) {
  }
}
相关问题