Android语音识别不起作用
Posted
技术标签:
【中文标题】Android语音识别不起作用【英文标题】:Android Speech Recognition not working 【发布时间】:2012-07-31 00:39:52 【问题描述】:我正在使用来自 newboston 的这个示例,它会提示我进行录制,但在它识别出我所说的内容后,它不会更新列表视图。
这里是代码。
public class MainActivity extends Activity
private static final int RECOGNIZER_RESULT = 1234;
ListView list;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.list);
Button btn_speach = (Button)findViewById(R.id.btn_speak);
btn_speach.setOnClickListener(new OnClickListener()
public void onClick(View v)
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech to search");
startActivityForResult(intent, RECOGNIZER_RESULT);
);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
if(requestCode == RECOGNIZER_RESULT && requestCode == RESULT_OK)
ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, matches));
for(int i = 0; i < matches.size(); i++)
Log.i("MainActivity", matches.get(i));
super.onActivityResult(requestCode, resultCode, data);
@Override
public boolean onCreateOptionsMenu(Menu menu)
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
这是 xml 布局。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_ >
<Button
android:id="@+id/btn_speak"
android:layout_
android:layout_
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button" />
<ListView
android:id="@+id/list"
android:layout_
android:layout_
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1" >
</ListView>
</RelativeLayout>
我没有收到任何错误。下面的代码也不会在 LogCat 中打印任何内容。
for(int i = 0; i < matches.size(); i++)
Log.i("MainActivity", matches.get(i));
我正在我的具有语音识别功能的安卓设备上进行测试。
【问题讨论】:
希望这篇博客能帮助您深入研究语音识别问题blog.contus.com/… 【参考方案1】:你可以试试这段代码……它正在我的应用程序中运行……
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class MainActivity extends Activity
Button speak;
ListView options;
ArrayList<String> results;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//doctory endsl...
speak = (Button) findViewById(R.id.bSpeak);
options = (ListView) findViewById(R.id.lvOptions);
speak.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
// This are the intents needed to start the Voice recognizer
Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
// i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
// RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
i.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 15); // number of maximum results..
i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Say something");
startActivityForResult(i, 1010);
);
// retrieves data from the previous state. This is incase the phones
// orientation changes
if (savedInstanceState != null)
results = savedInstanceState.getStringArrayList("results");
if (results != null)
options.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, results));
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
// TODO Auto-generated method stub
// retrieves data from the VoiceRecognizer
if (requestCode == 1010 && resultCode == RESULT_OK)
results = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
if (results.contains("close"))
finish();
//extra testing...
options.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, (results));
super.onActivityResult(requestCode, resultCode, data);
@Override
protected void onSaveInstanceState(Bundle outState)
// This should save all the data so that when the phone changes
// orientation the data is saved
super.onSaveInstanceState(outState);
outState.putStringArrayList("results", results);
xml 是...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@+id/lvOptions"
android:layout_
android:layout_
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1" >
</ListView>
<Button
android:id="@+id/bSpeak"
android:layout_
android:layout_
android:layout_centerHorizontal="true"
android:text="Speak" />
【讨论】:
以上是关于Android语音识别不起作用的主要内容,如果未能解决你的问题,请参考以下文章