语音识别android

时间:2013-04-17 10:31:24

标签: android voice-recognition

如何在 Android 上制作 speech to text 应用程序以持续工作 (每次我想说话时,没有点击按钮)? 我可以使用哪个活动?

谢谢,

3 个答案:

答案 0 :(得分:2)

您需要使用SpeechRecognizer

查看此课程中的recognizeDirectly方法: https://github.com/gast-lib/gast-lib/blob/master/library/src/root/gast/speech/SpeechRecognizingAndSpeakingActivity.java

警告:当前的Android api无法可靠地运行。它可能会持续记录一段时间然后崩溃。此外,它会在Android的更高版本中向您的用户发出哔哔声,这可能是不可取的。

答案 1 :(得分:0)

公共类MainActivity扩展了Activity {

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;
}

}

//它将解决您的问题

答案 2 :(得分:0)

您需要编写一个服务,该服务将在后台运行,您必须学习一个类 RecognitionService 和一个接口 RecognitionListener 欢呼声