我想开发一个实现语音识别的应用程序,然后使用文本到语音引擎实现文本到语音。我发布了下面的代码。我使用两个按钮和一个列表视图。一个按钮用于语音识别,另一个用于文本到语音,列表视图用于两者(列表视图中的第一个是发布语音识别的结果,然后应用程序将从列表视图中读回单词)。当我触摸用于语音识别的按钮时,单词会在我的列表视图中发布,但问题是当我按下按钮进行文本到语音时,应用程序不会从列表视图和我的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>
谢谢!
答案 0 :(得分:1)
您的TTS按钮没有注册onClickListener()。因此,从不调用启动TTS的代码。您确定要将ListView转换为String并将其传递给TTS引擎吗?您更有可能希望将ListView适配器中的数据转换为String。
答案 1 :(得分:1)
即使status == TextToSpeech.SUCCESS通过,也可能是语言数据不受支持。
在onInit()之后的某个时候,您需要执行一些如下所示的代码:
public void onInit(Context context, TextToSpeech tts, Locale forLocale)
{
Locale defaultOrPassedIn = forLocale;
if (forLocale == null)
{
defaultOrPassedIn = Locale.getDefault();
}
// check if language is available
switch (tts.isLanguageAvailable(defaultOrPassedIn))
{
case TextToSpeech.LANG_AVAILABLE:
case TextToSpeech.LANG_COUNTRY_AVAILABLE:
case TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE:
tts.setLanguage(defaultOrPassedIn);
break;
case TextToSpeech.LANG_MISSING_DATA:
Log.d(TAG, "MISSING_DATA");
// check if waiting....
Intent installIntent = new Intent();
installIntent
.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
context.startActivity(installIntent);
break;
case TextToSpeech.LANG_NOT_SUPPORTED:
Log.d(TAG, "NOT SUPPORTED");
// report failure to the user
break;
}
}
答案 2 :(得分:0)
您确实确实收到了结果吗?
在适配器中有一些元素吗?从起点调试始终。