2013-04-17 1 views

답변

2

이 클래스 내에서 recognizeDirectly 방법을 SpeechRecognizer

체크 아웃 사용할 필요가 : https://github.com/gast-lib/gast-lib/blob/master/library/src/root/gast/speech/SpeechRecognizingAndSpeakingActivity.java

주의 할을 : 현재 안드로이드 API는 안정적으로 작동하지 않습니다. 그것은 잠시 동안 계속 기록 될 것이고 충돌 할 것입니다. 또한 Android의 이후 버전에서는 사용자에게 경고음이 들리지만 바람직하지 않을 수 있습니다.

0

공용 클래스 MainActivity가 활동 {

private static final int REQUEST_CODE = 1234; 
private ListView resultList; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.activity_main); 
    resultList = (ListView) findViewById(R.id.list); 
    // to check if recognizer available or not 
    PackageManager pm = getPackageManager(); 
    List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(
      RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0); 
    if (activities.size() == 0) { 

     Toast.makeText(getApplicationContext(), "Recognizer Not Found", 
       1000).show(); 
    } 

    startVoiceRecognitionActivity(); 
} 

private void startVoiceRecognitionActivity() { 

    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, 
      RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, 
      "AndroidBite Voice Recognition..."); 
    intent.putExtra(
      RecognizerIntent.EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS, 
      5000000); 
    startActivityForResult(intent, 1234); 

} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) { 
     ArrayList<String> matches = data 
       .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 

     System.out.println("Matches list " + matches); 
     resultList.setAdapter(new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_1, matches)); 
    } 
    super.onActivityResult(requestCode, resultCode, data); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

}

를 확장 // 그것은 당신이 다시 그라운드에서 실행하는 서비스를 쓸 필요가 문제

+0

이 작업을 수행하려면 activity_main 레이아웃에 ID가 "List"인 ListView가 있어야합니다. –

0

를 해결할 것입니다, 당신은에 있습니다 한 반 공부 RecognitionService 한 인터페이스 RecognitionListener 건배