2011-04-19 204 views
4

我想开发一个实现语音识别的应用程序,然后使用文本语音引擎实现文本到语音。我发布了代码段。我使用两个按钮和一个列表视图。一个按钮用于语音识别,另一个用于文本到语音,而列表视图用于两者(首先在列表视图中张贴语音识别的结果,然后应用程序将从列表视图中读回单词)。当我触摸按钮进行语音识别时,单词会在我的列表视图中发布,但问题是,当我按下文本按钮来发言时,应用程序不会从列表视图和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

什么发生在logcat中,什么? – Jimmy 2011-04-19 16:11:57

回答

0

您是否确定其实际接收结果?

在你的适配器中有一些元素吗?从起点调试总是

1

您的TTS按钮没有注册onClickListener()。因此,您的代码启动TTS从不被调用。您确定要将ListView转换为字符串并将其传递给TTS引擎吗?您更希望将ListView的适配器中的数据转换为字符串。

+0

大声笑很好的搜索! – JoxTraex 2011-04-19 17:08:04

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