Android上的简单语音识别应用程序无法正常工作

时间:2017-04-26 16:42:57

标签: java android xml voice-recognition

我编码的简单语音识别应用程序无法正常工作。基本上,日志上没有可见的错误并且它编译得很好,但是当我点击按钮时,语音识别功能会弹出并开始录制,但是在完成录制后,应用程序屏幕上没有任何内容显示列表视图应该显示的内容记录的内容(文字格式)

这是java代码

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;


public class Voice extends Activity implements View.OnClickListener {

    ListView lv;
    final static int check = 1111;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.voice);
        lv = (ListView) findViewById(R.id.lvVoiceReturn);
        Button b = (Button) findViewById(R.id.bVoice);
        b.setOnClickListener(this);
    }



    @Override
    public void onClick(View v) {
        Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);        // set speech recognizer intent
        i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);  //set language
        i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak up son!");             // set prompt to user
        startActivityForResult(i, check);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == check && resultCode == RESULT_OK) {
            // create an empty array list and link it to the recognizer intent
            ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            // link the listview from my layout to the arraylist I created just now with the results from voice recognition
            lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results));
        }

    }
}

这是xml布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/bVoice"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Click to Speak" />

    <ListView
        android:id="@+id/lvVoiceReturn"
        android:layout_width="match_parent"
        android:layout_height="fill_parent" />
</LinearLayout>

谢谢!

1 个答案:

答案 0 :(得分:0)

@Kalid你可以尝试将语言设置为intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,&#34; en-US&#34;);并使用调试或日志检查应用程序控件是否转到onActivityResult