我想开发一个实现语音识别的应用程序,然后使用文本语音引擎实现文本到语音。我发布了代码段。我使用两个按钮和一个列表视图。一个按钮用于语音识别,另一个用于文本到语音,而列表视图用于两者(首先在列表视图中张贴语音识别的结果,然后应用程序将从列表视图中读回单词)。当我触摸按钮进行语音识别时,单词会在我的列表视图中发布,但问题是,当我按下文本按钮来发言时,应用程序不会从列表视图和logcat中读回单词我按下这个按钮我没有收到任何关于此的信息。 这里是我的程序:语音识别和文本到语音
package rtv.rtv.rtv;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
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;
import java.util.ArrayList;
import java.util.List;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;
public class VoiceRecTextSpeech extends Activity implements OnClickListener,OnInitListener {
private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
private ListView mList;
private Button speakBtn = null;
private static final int REQ_TTS_STATUS_CHECK = 0;
private static final String TAG = "TTS Demo";
private TextToSpeech mTts;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Button and ListView for Voice Recognition
Button speakButton = (Button) findViewById(R.id.btn_speak);
mList = (ListView) findViewById(R.id.list);
//Button for Text to Speech
speakBtn = (Button)findViewById(R.id.speak);
// Check to see if a recognition activity is present
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(
new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
if (activities.size() != 0) {
speakButton.setOnClickListener(this);
} else {
speakButton.setEnabled(false);
speakButton.setText("Recognizer not present");
}
// Check to be sure that TTS exists and is okay to use
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, REQ_TTS_STATUS_CHECK);
}
//Handle the click on the start recognition button and on text to speech button
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_speak:
startVoiceRecognitionActivity();
break;
case R.id.speak:
mTts.speak(mList.toString(), TextToSpeech.QUEUE_ADD, null);
break;
}
}
//Fire an intent to start the speech recognition activity
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, "Speech recognition demo");
startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
}
// Handle the results
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
// Fill the list view with the strings the recognizer thought it could have heard
ArrayList<String> matches = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
mList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
matches));
}
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQ_TTS_STATUS_CHECK) {
switch (resultCode) {
case TextToSpeech.Engine.CHECK_VOICE_DATA_PASS:
// TTS is up and running
mTts = new TextToSpeech(this, this);
Log.v(TAG, "Pico is installed okay");
break;
case TextToSpeech.Engine.CHECK_VOICE_DATA_BAD_DATA:
case TextToSpeech.Engine.CHECK_VOICE_DATA_MISSING_DATA:
case TextToSpeech.Engine.CHECK_VOICE_DATA_MISSING_VOLUME:
// missing data, install it
Log.v(TAG, "Need language stuff: " + resultCode);
Intent installIntent = new Intent();
installIntent.setAction(
TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
break;
case TextToSpeech.Engine.CHECK_VOICE_DATA_FAIL:
default:
Log.e(TAG, "Got a failure. TTS apparently not available");
}
}
else {
// Got something else
}
}
@Override
public void onInit(int status) {
// Now that the TTS engine is ready, we enable the button
if(status == TextToSpeech.SUCCESS) {
speakBtn.setEnabled(true);
}
}
@Override
public void onPause()
{
super.onPause();
// if we're losing focus, stop talking
if(mTts != null)
mTts.stop();
}
@Override
public void onDestroy()
{
super.onDestroy();
mTts.shutdown();
}
}
下面是main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="@+id/speak"
android:layout_height="wrap_content"
android:enabled="false" android:text="Text To Speech" android:layout_width="match_parent"/>
<Button android:id="@+id/btn_speak"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:text="Speak for Voice Recognition"/>
<ListView android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
</LinearLayout>
谢谢!
什么发生在logcat中,什么? – Jimmy 2011-04-19 16:11:57